- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我希望我的应用检测外部存储的状态是否发生变化。首先在我的 AndroidManifest 中定义了一个 BroadcastReceiver。在这里,我可以像这样设置 android:process
和 android:exported
属性:
<receiver android:name=".StorageStateReceiver" android:process=":storage_state" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
<data android:scheme="file" />
</intent-filter>
</receiver>
然后我意识到我只在一个 Activity 中使用这个接收器,所以不需要在应用程序启动时实例化它,而是我可以在代码中以编程方式定义它。这是我想出的:
BroadcastReceiver StorageStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Do what needs to be done
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
filter.addDataScheme("file");
getApplicationContext().registerReceiver(StorageStateReceiver, filter);
我将此代码放在 Activity 的 onCreate() 方法中。
但是我找不到从代码中设置process
的方法。我已阅读有关 BroadcastReceiver 和 Context 类的文档。 BroadcastReceiver 似乎没有托管任何让您定义进程名称的方法。另一方面,registerReceiver() 可以接受两个额外的参数:String broadcastPermission
、Handler scheduler
。 Handler 听起来很有前途,但我找不到可以接受字符串形式的进程名称的 Handler 构造函数。我觉得我的想法用完了。有没有办法以编程方式设置进程名称?
最佳答案
Then I realized that I only use this receiver in one single Activity, so there's no need to have it instantiated when the app starts, instead I can define it programmatically in code.
list 注册的 BroadcastReceiver
不会“在应用程序启动时实例化”。它仅在发送匹配的广播时实例化。
But I can't find a way to set process from code.
那是因为这是不可能的。此外,您不需要它,它会浪费 RAM、CPU 和电池,从而损害用户的利益。您也不应该在 list 条目中拥有 android:process
属性,除非您完全且准确地知道为什么您需要另一个进程。绝大多数 Android 应用都没有。
关于android - BroadcastReceiver:以编程方式设置 android:process,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16126030/
我的应用程序无法在 Android 7 上运行。调用了我的 BroadcastReceiver.onReceive 方法,但缺少 intent.getExtras 的内容。我已验证数据已正确加载。这是
我认为即使我退出应用程序,BroadcastReceiver AlarmManagerBroadcastReceiver 也会继续运行。我认为 BroadcastReceiver 会在我重新启动系统时
我为 Wi-Fi 创建了一个 brodcastReceiver,一切正常,除了,当我午餐应用程序时,broadcastreceiver 开始直接扫描,我无法停止它,即使我离开应用。我试图从主要 Act
我最近开始在我的 Android 应用程序上使用 Google Analytics(分析)。我想知道如何在 BroadcastReceiver 的 onReceive 方法中将事件发送到 GA。这是一
我想在 Broadcast Receiver 中检查互联网连接;并将结果( bool 标志)设置为全局变量,以便在 if 条件下将其用于整个应用程序;如果互联网断开连接,则在主要 Activity 中
如果用户向上或向下按下音量键,是否可以在我的广播接收器中检测到它?我需要完整的代码。 这是我的 Intent 过滤器 IntentFilter filter = new IntentFilter();
我正在尝试编写一个应用程序,其中我根据使用 gcm 推送通知发送的消息对 UI 进行更改,并且我设法通过使用 BroadcastReceiver onReceive 函数来实现它来实现它,但它仅在应用
我正在尝试使用 AlarmManager 安排通知当我安排一个通知时它工作得很好,但是当我安排两个通知时,第一个通知没问题,但第二个通知不起作用。 我发现几分钟后打开应用程序会通知第二个通知。我认为我
我正在阅读指南 here关于设置 BroadcastReceiver 来检查电池变化。它指出我可以像这样设置一个 BroadcastReceiver:
我想在应用程序中接收有关启用/禁用位置提供程序的信息,例如NETWORK、GPS等。我创建了简单的BroadcastReceiver: public class TestReciver extends
我正在使用广播接收器来设置警报警报,但由于某种原因它无法工作。 public class SimpleSleepActivity extends Activity { /** Called when
当我的应用程序处于后台时,我尝试播放一些音调,并按下“相机”按钮,因为我正在执行此简单的步骤。 创建BroadcastReceiver类 public class CameraButtonListen
我创建了我的广播接收器 AnprEventReciever,它应该在连接状态发生变化时被触发,但它并没有。 事件接收者: import android.content.BroadcastReceive
我有一个时间选择器来设置闹钟 public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
我目前正在开发一个使用 broadcastreceiver 来检查收到的短信的应用程序,但突然间它似乎停止工作了,我什至写了一个小的测试应用程序,以至少对我来说,语法上似乎是正确的,但也不起作用。 这
我正在尝试使用 BroadcastReceiver 获取互联网连接状态。 接收类是这样的: public class ConnectivityReceiver extends BroadcastRec
我有一个小部件,它只使用服务来更新小部件的 RemoteView 的外观。该服务基于由不同注册的 BroadcastReceiver 提供的过滤器启动。当满足其中一个条件时,将调用我的 Broadca
我没有时间玩接收器,而且我对2.3.x以上的东西都不熟悉,所以当我读到这个问题时,我感到非常困惑: Can BroadcastReceiver registered in AndroidManifes
我有一个录音机 android 线程,我需要知道录音时麦克风/耳机是否已连接,所以我需要在线程内使用 BroadcastReceiver()。我如何注册它? this.registerReceiver
大家好,感谢您的帮助!我将从代码开始 所以,带有广播接收器的类是这样的: public class MyService extends Service { // ... // ACTI
我是一名优秀的程序员,十分优秀!