【问题标题】:Android Background Service request php and send notificationAndroid后台服务请求php并发送通知
【发布时间】:2017-08-22 16:32:09
【问题描述】:

我有点困惑,为什么我无法为我的问题找到结果。

我想创建一个后台服务,它请求一个 php 文件返回一个 json 字符串,然后发送一个包含此 json 字符串内容的通知。

所以我可以向我的用户发送类似的通知。

这怎么可能。

当时的 Windows Phone 就像一个定期运行 30 分钟的后台服务,当我想发送即时通知时,我必须使用那里的服务。

【问题讨论】:

  • Android 和谷歌提供了所有的好东西来做到这一点。来吧,谷歌它!因为它对于这种格式来说太宽泛了。如果您遇到特定的编程问题/问题,请稍后重新打开一个问题。

标签: php android download background-service


【解决方案1】:

您可以使用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;
        } 
    } 
} 

希望对您有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多