![](/img/trans.png)
[英]How can I determine if a broadcast receiver was invoked when the app was running as opposed to when the app was dormant or in the background?
[英]How can I write a Broadcast Receiver that will be invoked when user clicks on any application icon?
我如何編寫當用戶單擊任何應用程序圖標時將被調用的廣播接收器?
我嘗試通過寫:
<receiver android:name = "myreceiver">
<intent-filter>
<action android:name = "android.intent.action.MAIN">
</intent-filter>
</receiver>
但是它沒有被調用。
我嘗試過,通過使用Packagemanager
我將獲得ApplicationInfo
。 從中我可以知道所有應用程序的起始活動名稱和程序包名稱。 我以為可以用它們注冊registerReceiver,並且我的接收器將通過其啟動活動和程序包名稱進行偵聽。 但是我很震驚。 我做不到。 我想我迷路了。
我該怎么做才能解決這個問題?
我認為這是不可能的,存在太多的濫用空間。 您正在嘗試做什么,以便在有人啟動應用程序時需要通知您?
使用此技術:
//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
}
基本上就是這個。 只要您按一下按鈕,就會發送廣播。 您的廣播接收器可以在其他地方編碼,以監聽傳入的廣播。 如果您需要廣播發送端或廣播接收端的更多代碼,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.