广播里的活动接收器 [英] broadcast receiver inside activity

查看:126
本文介绍了广播里的活动接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计我怎么能实现在与一些参数的int和string类型?

沿着接收来自服务的意图活动的广播接收器

更新:

我有这样的活动下:

 私人广播接收器ReceivefrmSERVICE =新的广播接收器(){    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        Toast.makeText(背景下,IN DA广播电台,
                Toast.LENGTH_LONG).show();    }   };

和我有这样的服务功能时,选中的按钮被选中时调用从另一个活动的事件下:

 公共无效switchSpeaker(INT小时,诠释分钟){       Toast.makeText(Server.this,小时+,+最小值,Toast.LENGTH_LONG).show();       意向意图=新意图(这一点,andRHOME.class);
       //intent.putExtra(\"sendMessage\",\"1);
        startActivity(意向);
        / *的PendingIntent圆周率= PendingIntent.getService(Server.this,0,myIntent,0);
        AlarmManager almmgr =(AlarmManager)getSystemService(ALARM_SERVICE);
        日历CLDR = Calendar.getInstance();
        INT分1 = cldr.get(Calendar.MINUTE);
        cldr.setTimeInMillis(System.currentTimeMillis的());
        cldr.add(Calendar.SECOND,30);
        almmgr.set(AlarmManager.RTC_WAKEUP,cldr.getTimeInMillis(),PI); * /
    }

但它的崩溃? ,该怎么办?


解决方案

 广播接收器接收器=新的广播接收器(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
       //做你STU​​FF
    }
}IntentFilter的过滤器=新的IntentFilter();

在onResume:(所以你只能得到,而在前台)

  filter.addAction(/ *您希望收到* /动作);
registerReceiver(接收器,过滤器);

在的onPause(在尝试捕捉是修复在注销错误的情况下,它被称为两次)

  {尝试
    unregisterReceiver(接收机);
}赶上(抛出:IllegalArgumentException五){
    如果(e.getMessage()。包含(接收未注册)){
        //忽略此异常。这正是需要
        Log.w(TAG,试图注销时,它没有注册reciver);
    }其他{
        //意外,再次引发
        Ë扔掉;
    }
}

Hi folks how I can implement a Broadcast receiver in an activity that receives intent from a service along with some parameter's of int and string type?

UPDATE:

I have this under the Activity:

 private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){

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

        Toast.makeText(context, "IN DA BroadCASTER",
                Toast.LENGTH_LONG).show();

    }

   };

and I have this under a function in the service that is called on a event from another activity when a checked button is checked:

 public void switchSpeaker(int hr, int min){

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

       Intent intent = new Intent(this, andRHOME.class);
       //intent.putExtra("sendMessage","1");
        startActivity(intent);
        /*PendingIntent pi = PendingIntent.getService(Server.this, 0, myIntent, 0);
        AlarmManager almmgr = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar cldr = Calendar.getInstance();
        int min1 = cldr.get(Calendar.MINUTE);
        cldr.setTimeInMillis(System.currentTimeMillis());
        cldr.add(Calendar.SECOND, 30);
        almmgr.set(AlarmManager.RTC_WAKEUP, cldr.getTimeInMillis(), pi);*/
    }

But its crashing?? ,What to do?

解决方案

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {         
       // DO YOUR STUFF
    }
}

IntentFilter filter = new IntentFilter();

in onResume: (so you receive only while in foreground)

filter.addAction(/* the action you want to receive */);
registerReceiver(receiver, filter);

in onPause: (the try catch is to fix a bug in unregister in case it is called twice)

try {
    unregisterReceiver(receiver);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("Receiver not registered")) {
        // Ignore this exception. This is exactly what is desired
        Log.w(TAG,"Tried to unregister the reciver when it's not registered");
    } else {
        // unexpected, re-throw
        throw e;
    }
}

这篇关于广播里的活动接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆