如何利用广播接收器上接收事件 [英] how to use broadcast receiver on receive event

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

问题描述

我有一个项目,运行应用程序时,一个服务是活动的,当设备上我的Andr​​oid服务轮到被激活。
该项目在设备的内存中成功运行,但是当我在外部存储器安装它,重新启动我的设备再次不工作!

对于第一次我打电话给我的第一活性的研究服务和它的作品,但是当我重新启动我的设备,它不工作!

我的活动:

 公共类的Firstclass扩展活动
   {
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.first);
    最后的处理程序处理程序=新的处理程序();
    handler.postDelayed(新的Runnable()
    {
        公共无效的run()
        {
            startService(新意图(getApplicationContext(),MyService.class));
            startActivity(新意图(FirstClass.this,MainActivity.class));
            完();
        }
    },5000);
}
/////////////////////////////////////////////////
   }

我的广播接收器:

 公共类BroadcastReceiverOnTurnedOn扩展广播接收器{
 @覆盖
  公共无效的onReceive(上下文的背景下,意图意图){
      意图startServiceIntent =新意图(背景下,MyService.class);
      context.startService(startServiceIntent);
  }
 }

我说:

 <服务
        机器人:名字=com.dariran.MyService
        机器人:启用=真
        机器人:出口=真正的>
    < /服务>
    <接收机器人:名字=com.dariran.Bro​​adcastReceiverOnTurnedOn
        机器人:启用=真正的>
        <意向过滤器的android:优先=1>
            <作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
        &所述; /意图滤光器>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE/>
        &所述; /意图滤光器>
    < /接收器>

上的Manifest.xml appliation标签以及

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
   <使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>


解决方案

我添加了这个$​​ C $ C到我的服务类把一个过滤器来识别外部存储,但不要再工作:(

  @覆盖
公共无效调用onStart(意向意图,诠释startId){
    尝试{        IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        广播接收器mReceiver =新BroadcastReceiverOnTurnedOn();
        registerReceiver(mReceiver,过滤器);    }赶上(例外五){
    }
}

i have a project that when run application one service be active and when device be turn on my android service be active. the project run in device's internal memory successfully but when i install it on external memory and reboot my device again don't work!

for first i call my service in first acitivity and it works, but when i reboot my device it doesn't work !

my activity :

   public class FirstClass extends Activity 
   {
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first);
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() 
    {
        public void run()
        {
            startService(new Intent(getApplicationContext(), MyService.class));
            startActivity(new Intent(FirstClass.this, MainActivity.class));
            finish();
        }
    },5000);
}
/////////////////////////////////////////////////
   }

my broadcast receiver :

 public class BroadcastReceiverOnTurnedOn extends BroadcastReceiver {
 @Override
  public void onReceive(Context context, Intent intent) {
      Intent startServiceIntent = new Intent(context, MyService.class);
      context.startService(startServiceIntent);
  }
 }

i added :

    <service
        android:name="com.dariran.MyService"
        android:enabled="true"
        android:exported="true" >
    </service>
    <receiver android:name="com.dariran.BroadcastReceiverOnTurnedOn"
        android:enabled="true">
        <intent-filter android:priority="1">
            <action android:name="android.intent.action.BOOT_COMPLETED" /> 
        </intent-filter>
        <intent-filter>
            <action  android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
        </intent-filter>
    </receiver>

to appliation tag on Manifest.xml and

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

解决方案

i added this code to my service class to put a filter to recognize the external storage but don't work again :(

    @Override
public void onStart(Intent intent, int startId) {
    try {

        IntentFilter filter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        BroadcastReceiver mReceiver = new BroadcastReceiverOnTurnedOn();
        registerReceiver(mReceiver, filter);

    } catch (Exception e) {
    }
}

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

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