Android Background Service запросит php и отправит уведомление

Я немного запутался, почему не смог найти результат по моему вопросу.

Я хочу создать фоновую службу, которая запрашивает php-файл, возвращает строку json, а затем отправляет уведомление с содержимым из этой строки json.

Поэтому я могу отправлять подобные уведомления своим пользователям.

Как это возможно.

В то время Windows Phone имел периодическую фоновую службу, работавшую все 30 минут, и когда я хотел отправлять мгновенные уведомления, мне приходилось использовать там службу.

0

Решение

Ты можешь использовать Volley.

Вы можете использовать этот код в качестве ссылки:

package com.exampfle.real;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class RealService extends Service{

private static final String TAG="RealService";
private boolean isRunning=false;
private IBinder mBinder=new MyBinder();
private boolean intenetAccess=false;
/* Change here */
private RequestQueue reQueue;
private final String url="http://www.google.com";

public boolean SendRequest()
{
/* Change here */
reQueue = Volley.newRequestQueue(this);
StringRequest request=new StringRequest(com.android.volley.Request.Method.GET,
url,
new Response.Listener<String>() {

@Override
public void onResponse(
String response) {

intenetAccess=true;
}
},

new Response.ErrorListener() {

@Override
public void onErrorResponse(
VolleyError error) {

intenetAccess=false;

}
});

try{
reQueue.add(request);
}
catch(Exception e){}

return intenetAccess;

}

@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service onCreate");

isRunning=true;

}@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "Service onBind");
return mBinder;
}

@Override
public void onRebind(Intent intent) {
Log.i(TAG, "Service onRebind");
super.onRebind(intent);
}

@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "Service onUnBind");
return true;
}

@Override
public void onDestroy() {

isRunning=false;

Log.i(TAG, "Service onDestroy");
super.onDestroy();
}public class MyBinder extends Binder
{
RealService getService()
{
return RealService.this;
}
}
}

Надеюсь, это помогло вам 🙂

0

Другие решения

Других решений пока нет …