远程报警广播 通信数据

Remote Alarm Broadcast Communicating data

我需要一个定期轮询机制来检测新订单(在用户看到的最后一个订单之后) 我已经设置了几个广播接收器(一个用于启动激活,另一个用于执行重复任务)和一个 service.I 使用 Alarmmanager 定期调用 PeriodicTaskReciever.

现在我需要知道如何与这样的远程广播接收器通信(指示最后用户看到的订单的标志参数需要更新)。我试过共享首选项,正如很多文章中指出的那样,远程进程无法读取 SP

的更新值
<service android:name=".OrderService" />
        <receiver android:name=".BootReciever" >
            <intent-filter>    
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

    <receiver android:name=".PeriodicTaskReciever"
        android:process=":remote"
        />  

我的 PeriodicTask 接收者是

public class PeriodicTaskReceiver extends BroadcastReceiver
{ private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    static int lastOrder;
    int NOTIFICATION_COUNTID=100;
    public void onReceive(final Context context, Intent intent)
    {
        Log.d("DataHandlerService",  "Service hbeat");
        if(intent.getIntExtra("update",0)!=0){
            lastOrder=intent.getIntExtra("update",0);
            Log.d("DataHandlerService",  "Last order updated");
        }
        else{
            if(Utils.isNetworkAvailable(AppController.getInstance().getApplicationContext())){
                Uri.Builder uri = new Uri.Builder();
                uri.scheme("http");
                uri.authority(Constants.URL_API_HOST);
                uri.path(Constants.URL_API_ORDERPATH);


                uri.appendQueryParameter("lastorder",getLastOrderFromSharedPref());
                String url=uri.build().toString();
                Log.d("DataHandlerService",  "Service hbeat"+url);

                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(url,null,new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            if(!jsonObject.isNull("status") && jsonObject.getString("status").equals("success")){

                                if(Integer.parseInt(jsonObject.getString("data"))>0){

                                    displayNotification("You have "+ Integer.parseInt(jsonObject.getString("data"))+ " new orders ",context);
                                }}
                        } catch (Exception e) {

                        }


                    }
                },new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });

                AppController.getInstance().addToRequestQueue(jsonObjectRequest,"PollOrders");}}
    }

    public void SetAlarm(Context context)
    {
        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, PeriodicTaskReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 , pi); // Millisec * Second * Minute
    }

    public void CancelAlarm(Context context)
    {
        Intent intent = new Intent(context, PeriodicTaskReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }

我是不是误用了遥控器。当我的应用程序未 运行 时,我需要从后台通知用户。一旦用户点击通知或将其关闭,lastOrder 值就会更新。

BroadcastReceiver 没有 android:process=":remote" 的作品。所以删除它,这里你没有使用 intent-filter 作为接收器,所以你需要 exportBroadcastReceiver.


而不是

<receiver android:name=".PeriodicTaskReciever"
        android:process=":remote"
        />  

导出你的接收器。喜欢

<receiver android:name=".PeriodicTaskReciever"
        android:exported="true"
        />