我试图创建一个简单的BroadcastReceiver,它可以接收android.intent.action.BOOT_COMPLETED intent以及android.intent.action.MEDIA_MOUNTED intent。这个想法是开始接受这些意图的服务。因此,应在Android引导完成或USB存储设备连接到Android目标(如果服务尚未启动)之后启动该服务。由应用程序使用 权限将在本节Broadcast Receiver not receiving BOOT and MEDIA_MOUNTED intent
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
以下部分定义负责处理意图和起动相应的服务的广播接收器定义。
<receiver
android:name="com.example.systemupgradeapplication.IntentReceiver"
android:label="USB Detection Receiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
<intent-filter android:priority="999" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
应该从广播接收器可以启动相应的服务在以下部分中定义
<service android:name=".SysUpgradeService" />
注:接收器和服务组件Android清单的<application>
节中定义的。
下面的片段是广播负责处理意图类
public class IntentReceiver extends BroadcastReceiver {
private final static String TAG = "IntentReceiver";
private static boolean m_UsbInserted = false;
private static boolean m_UsbRemoved = true;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "LaunchReceiver::ACTION_MEDIA_MOUNTED :: intent received with path= ");
// TODO Auto-generated method stub
//if(intent.)
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
String path = intent.getDataString();
if(path.contains("usb")) {
m_UsbInserted = true;
Log.d(TAG, "LaunchReceiver::ACTION_MEDIA_MOUNTED :: intent received with path= "+path);
Intent myIntent = new Intent(context, SysUpgradeService.class);
myIntent.putExtra("path", path);
context.startService(myIntent);
}
}else if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d(TAG, "BOOT Completed intent received");
context.startService(new Intent(context, SysUpgradeService.class));
}
我现在面临的问题是,没有任何意图都在我的广播接收器到达的IntentReceiver类(日志中没有被即使我可以在Android Debug日志中看到正在广播BOOT_COMPLETE和MEDIA_MOUNTED意图,也可以在logcat中打印)。此外,此应用程序不启动后,Android系统启动。
感谢您在这方面的帮助,我的方法和一些可能的解决方案可能有什么问题。
是的,它让我等待2天,直到我可以接受我自己的答案。感谢提醒@DavidWasser – SCCC 2015-01-23 12:09:18