简体   繁体   English

我如何编写当用户单击任何应用程序图标时将被调用的广播接收器?

[英]How can I write a Broadcast Receiver that will be invoked when user clicks on any application icon?

How can I write a Broadcast Receiver that will be invoked when user clicks on any application icon? 我如何编写当用户单击任何应用程序图标时将被调用的广播接收器?

I tried by writing: 我尝试通过写:

<receiver android:name = "myreceiver"> 
    <intent-filter> 
        <action android:name = "android.intent.action.MAIN"> 
    </intent-filter> 
</receiver> 

But it is not called. 但是它没有被调用。

I tried, by using Packagemanager I will get ApplicationInfo . 我尝试过,通过使用Packagemanager我将获得ApplicationInfo From that I can know all the application starting activity name and package names. 从中我可以知道所有应用程序的起始活动名称和程序包名称。 I thought I can use them to registerReceiver and my receiver will listen by its launching activity and package name. 我以为可以用它们注册registerReceiver,并且我的接收器将通过其启动活动和程序包名称进行侦听。 But I strucked. 但是我很震惊。 I am unable to do that. 我做不到。 I think I lost the way. 我想我迷路了。

What can I do to solve this problem? 我该怎么做才能解决这个问题?

I don't think this is possible, there is just too much room for abuse. 我认为这是不可能的,存在太多的滥用空间。 What are you trying to do that you would need to be notified anytime someone launched an application? 您正在尝试做什么,以便在有人启动应用程序时需要通知您?

Use this technique: 使用此技术:

//implement onClickListener on your class

Class blabla extends Activity implements onClickListener{
...
...
}

//this will force you to override the onClick method on your activity or //fragment
@override
void onClick(View V){
sendBroadcast(new Intent("clickSomewhere!"));
//Put here more actions you want to do when anything is touched/clicked
}

Basically this is it. 基本上就是这个。 Anytime you press a button the Broadcast will be sent. 只要您按一下按钮,就会发送广播。 Your Broadcast receiver, could be coded in some other place, listening to incoming broadcasts. 您的广播接收器可以在其他地方编码,以监听传入的广播。 If you need more code of the broadcast sending side, or the broadcast receiving side let me know. 如果您需要广播发送端或广播接收端的更多代码,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM