开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜