广播接收器未收到 BOOT 和 MEDIA_MOUNTED 意图
Broadcast Receiver not receiving BOOT and MEDIA_MOUNTED intent
我正在尝试创建一个简单的 BroadcastReceiver,它可以接收 android.intent.action.BOOT_COMPLETED 意图以及 android.intent.action.MEDIA_MOUNTED 意图。这个想法是在收到这些意图中的任何一个时启动服务。因此,该服务应在 Android 启动完成后或 USB 存储设备连接到 Android 目标时启动(如果届时该服务尚未启动)。
应用程序使用的权限在本节中定义
<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" />
下面的部分定义了 BroadcastReceiver 负责处理 intents 并启动相应的服务。
<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>
部分中定义。
以下代码段是Class负责处理广播的意图
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));
}
我面临的问题是 none 的意图到达我的广播接收器(None IntentReceiver class 中的日志正在 logcat) 尽管我可以在 Android 调试日志中看到正在广播 BOOT_COMPLETE 和 MEDIA_MOUNTED 意图。此外,此应用程序在 android 系统启动后未启动。
感谢您在这方面的帮助,我的方法可能有什么问题以及一些可能的解决方案。
确保您的 Application.From Android 中至少有一个 activity 3.1,在用户手动启动 activity 之前,BroadcastReceiver 将无法工作,这是为提供安全。一旦用户 运行 第一次使用该应用程序,您的 BroadcastReceiver 将始终 运行 除了它不会强制停止它。一旦 activity 首次启动,您的广播接收器将 运行 即使在重新启动您的设备后也是如此。
好的,所以我将 apk 推送到 /system/priv-app,这是放置作为自定义 ROM 一部分的系统应用程序的地方。现在我的应用程序中不需要任何 activity,因为它是自定义 ROM 的一部分并且被识别为系统应用程序。似乎如果您的应用程序是第 3 方应用程序,它必须具有 activity 才能接收广播的意图。
但是在这种情况下,我可以控制自定义 ROM 源代码以及设备上的根访问权限。所以这两种方法都有效
- 使您的应用程序成为自定义 ROM 源的一部分,在设备上构建和闪存。
- 在设备上获取 root 访问权限,将您的 apk 推送到 /system/priv-app(4.4 以后),重新启动,瞧!
我正在尝试创建一个简单的 BroadcastReceiver,它可以接收 android.intent.action.BOOT_COMPLETED 意图以及 android.intent.action.MEDIA_MOUNTED 意图。这个想法是在收到这些意图中的任何一个时启动服务。因此,该服务应在 Android 启动完成后或 USB 存储设备连接到 Android 目标时启动(如果届时该服务尚未启动)。 应用程序使用的权限在本节中定义
<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" />
下面的部分定义了 BroadcastReceiver 负责处理 intents 并启动相应的服务。
<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>
部分中定义。
以下代码段是Class负责处理广播的意图
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));
}
我面临的问题是 none 的意图到达我的广播接收器(None IntentReceiver class 中的日志正在 logcat) 尽管我可以在 Android 调试日志中看到正在广播 BOOT_COMPLETE 和 MEDIA_MOUNTED 意图。此外,此应用程序在 android 系统启动后未启动。
感谢您在这方面的帮助,我的方法可能有什么问题以及一些可能的解决方案。
确保您的 Application.From Android 中至少有一个 activity 3.1,在用户手动启动 activity 之前,BroadcastReceiver 将无法工作,这是为提供安全。一旦用户 运行 第一次使用该应用程序,您的 BroadcastReceiver 将始终 运行 除了它不会强制停止它。一旦 activity 首次启动,您的广播接收器将 运行 即使在重新启动您的设备后也是如此。
好的,所以我将 apk 推送到 /system/priv-app,这是放置作为自定义 ROM 一部分的系统应用程序的地方。现在我的应用程序中不需要任何 activity,因为它是自定义 ROM 的一部分并且被识别为系统应用程序。似乎如果您的应用程序是第 3 方应用程序,它必须具有 activity 才能接收广播的意图。 但是在这种情况下,我可以控制自定义 ROM 源代码以及设备上的根访问权限。所以这两种方法都有效
- 使您的应用程序成为自定义 ROM 源的一部分,在设备上构建和闪存。
- 在设备上获取 root 访问权限,将您的 apk 推送到 /system/priv-app(4.4 以后),重新启动,瞧!