addAction()&服务呼唤困境

时间:2012-12-15 06:28:10

标签: android

我在以前的斗争中被困在这里>> Prev. Struggle!

Raanan有帮助!我很多,但后来我觉得因为时区不同而离开了,现在我被我用来调用活动中的BroadcastReceiver()的服务代码所困扰了!而且我没有得到我应该加载filter.addAction(action)的参数;取代行动??

Kinldy指导我!

服务器中的代码:

Toast.makeText(Server.this, hr +" , " +min, Toast.LENGTH_LONG).show();

       Intent intent = new Intent(this, andRHOME.class);
       //intent.putExtra("sendMessage","1");
       sendBroadcast(intent);

和活动中的代码(广播接收器)

 private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "IN DA BroadCASTER",
                Toast.LENGTH_LONG).show();
                sendMessage("1");
    }
   };
    IntentFilter filter = new IntentFilter();

1 个答案:

答案 0 :(得分:1)

你需要将这些行添加到你的接收器的regiester中以进行某些操作,例如定义一个像这样的全局变量:

public static String NOTIFCATION_BROADCAST_ACTION = "com.your_packagename.UPDATE_NOTIFICATION_INTENT";

然后在activity onCreate()方法中注册这样的操作。

 IntentFilter filter = new IntentFilter();
 filter.addAction(Global.NOTIFCATION_BROADCAST_ACTION);
 registerReceiver(ReceivefrmSERVICE, filter);

然后从你的service发送广播

    Intent broadcast = new Intent();
    broadcast.setAction(Global.NOTIFCATION_BROADCAST_ACTION);
    sendBroadcast(broadcast);

然后在您的broadcast Receiver过滤器中执行此操作

 private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Global.NOTIFCATION_BROADCAST_ACTION)) {
                //Do your stuff here :)
            }
    }
   };