简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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