簡體   English   中英

BroadcastReceiver onReceive沒有被調用

[英]BroadcastReceiver onReceive is not getting called

我在AndroidManifest.xml中定義了一個BroadCastReceiver,如下所示

<receiver
   android:name="com.example.hello.ScreenUnlockReceiver" 
   android:enabled="true" 
   android:singleUser="true">
   <intent-filter>
      <action android:name="android.content.Intent.ACTION_USER_PRESENT" />
   </intent-filter>
</receiver>

並將接收者定義如下:

public class ScreenUnlockReceiver 
      extends BroadcastReceiver {


   @Override
   public void onReceive(Context context, Intent intent) {
       //start activity
       Intent i = new Intent();
       i.setClassName("com.example.hello", "LoginActivity");
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(i);
   }
}

但是當我解鎖屏幕並且沒有顯示LoginActivity時,不會觸發廣播接收器。 LoginActivity是android sdk附帶的默認loginactivity。

我是否遺漏了使用許可或其他內容,請告訴我。 謝謝

Santhosh

你應該像這樣解決問題
i.setClassName("com.example.hello", "com.example.hello.LoginActivity")

你必須攔截的動作是:

<intent-filter>
  <action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>

開發者Android ACTION_USER_PRESENT

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM