如何使用广播接收器与服务 [英] how to use broadcast receiver with service

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

问题描述

我该如何使用广播接收机和服务两​​者,前提是应用程序安装在外接存储。我编程,但只是在内部存储设备上运行,因为我希望在设备重新启动服务运行,而不是启动活动

我的活动:

 公共类的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扩展的BroadcastReceiver {
  @覆盖
  公共无效的onReceive(上下文的背景下,意图意图){
  意图startServiceIntent =新的意图(背景下,MyService.class);
  context.startService(startServiceIntent);
   }
 }
 

我说:

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

上的Manifest.xml蒋云良标签和 我说这code我的服务类把一个过滤器来识别外部存储但不工作了:(

  @覆盖
 公共无效ONSTART(意向意图,诠释startId){
 尝试 {

 IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
 BroadcastReceiver的mReceiver =新BroadcastReceiverOnTurnedOn();
 registerReceiver(mReceiver,过滤器);

 }赶上(例外五){
 }
}
 

解决方案

引用的文档

  

为了让您的应用程序始终像预期的那样,你不应该让你的应用程序能够在如果它使用以下任一功能的外部存储装...

     

广播接收器监听启动完成
     该系统提供到ACTION_BOOT_COMPLETED广播外部存储被安装到该装置之前。如果您的应用程序安装在外部存储,它永远不能接受这个广播。

在<一个其他地方报价href="http://developer.android.com/reference/android/content/Intent.html#ACTION_EXTERNAL_APPLICATIONS_AVAILABLE"相对=nofollow>的文件,这个时候 ACTION_EXTERNAL_APPLICATIONS_AVAILABLE

  

额外的数据EXTRA_CHANGED_UID_LIST包含包的可用性改变的UID的列表。注意,在这个列表中的包不接收这个广播

因此​​,你需要设置你的应用程序无法在外部存储安装。

how can i use broadcast receiver and service both of them, provided that app install on external storage. i programmed it but just run on internal storage device because i want when device rebooted service run instead start activity

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 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) {
 }
}

解决方案

Quoting the documentation:

In order for your application to consistently behave as expected, you should not allow your application to be installed on the external storage if it uses any of the following features...

Broadcast Receivers listening for "boot completed"
The system delivers the ACTION_BOOT_COMPLETED broadcast before the external storage is mounted to the device. If your application is installed on the external storage, it can never receive this broadcast.

Quoting elsewhere in the documentation, this time for ACTION_EXTERNAL_APPLICATIONS_AVAILABLE:

The extra data EXTRA_CHANGED_UID_LIST contains a list of uids of packages whose availability changed. Note that the packages in this list do not receive this broadcast.

Hence, you will need to set up your app to not be installed on external storage.

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

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