广播接收器:设置机器人:编程过程 [英] BroadcastReceiver: set android:process programmatically

查看:406
本文介绍了广播接收器:设置机器人:编程过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的应用程序来检测,如果当外部存储的状态发生改变。起初定义在我AndroidManifest一个BroadcastReceiver。在这里,我可以设置安卓过程的android:出口属性是这样的:

 <接收机器人:StorageStateReceiverNAME =机器人:工艺=:storage_state机器人:出口=假>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MEDIA_UNMOUNTED/>
            <作用机器人:名字=android.intent.action.MEDIA_MOUNTED/>
            <作用机器人:名字=android.intent.action.MEDIA_EJECT/>
            <作用机器人:名字=android.intent.action.MEDIA_BAD_REMOVAL/>
            <数据机器人:计划=文件/>
        &所述; /意图滤光器>
    < /接收器>

然后我意识到我只在一个单一的活动中使用该接收机中,所以没有必要把它实例化时应用程序启动时,而不是我可以通过编程在code定义它。这是我想出了:

 广播接收器StorageStateReceiver =新的广播接收器(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            //做需要做的事情
        }
    };
IntentFilter的过滤器=新的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(文件);
。getApplicationContext()registerReceiver(StorageStateReceiver,过滤器);

我把这个code中的onCreate()我的活动的方法。

但我不能找到一种方法来设置过程从code。我读过关于广播接收器和上下文类的文档。广播接收器似乎并没有举办,让你定义进程名称的任何方法。 registerReceiver(),另一方面可以acceps两个额外的参数:字符串broadcastPermission 处理器调度。处理器听起来前途,但我无法找到一个处理程序的构造函数,将一个字符串的形式接受进程名。我觉得我跑的想法。有没有一种方法来设置进程名编程?


解决方案

  

然后我意识到我只在一个单一的活动中使用该接收机中,所以没有必要把它实例化时应用程序启动时,而不是我可以通过编程在code定义它。


一个清单注册广播接收器不是实例化时应用程序启动。被实例化,只有当一个匹配的广播被发送。


  

但我不能找到一种方法来设置从code工艺。


这是因为它是不可能的。此外,你不需要它,它被浪费RAM,CPU和电池危害用户。你不应该有过的android:在清单录入过程属性,或者说,除非你知道的完全和precisely的为什么你需要另一个进程。绝大多数的Andr​​oid应用程序的不知道。

I want my app to detect if when the state of external storage changes. At first defined a BroadcastReceiver in my AndroidManifest. Here I can set android:process and android:exported attributes like this:

    <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>

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. This is what I came up with:

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);

I put this code in onCreate() method of my activity.

But I can't find a way to set process from code. I've read documentation on BroadcastReceiver and Context classes. BroadcastReceiver doesn't seem to host any methods that let you define process name. registerReceiver() on the other hand can acceps two extra arguments: String broadcastPermission, Handler scheduler. Handler sounds promising, but I couldn't find a Handler constructor that would accept process name in a form of a string. I feel that I ran out of ideas. Is there a way to set process name programmatically?

解决方案

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.

A manifest-registered BroadcastReceiver is not "instantiated when the app starts". It is instantiated only when a matching broadcast is sent.

But I can't find a way to set process from code.

That is because it is impossible. Besides, you do not need it, and it harms the user by wasting RAM, CPU, and battery. You should not have had the android:process attribute in the manifest entry, either, unless you know completely and precisely why you need another process. The vast majority of Android apps do not.

这篇关于广播接收器:设置机器人:编程过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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