Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver onReceive is not getting called

I have defined a BroadCastReceiver in AndroidManifest.xml as below

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

and defined the Receiver as below :

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);
   }
}

But the broadcastreceiver is not triggered when I unlock the screen and the LoginActivity is not being shown. LoginActivity is the default loginactivity which comes with android sdk.

Am I missing something in uses-permission or something else, please let me know. Thanks

Santhosh

like image 681
Santhosh S Avatar asked Feb 16 '23 15:02

Santhosh S


2 Answers

The action you have to intercept is:

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

Developer Android ACTION_USER_PRESENT

like image 183
rciovati Avatar answered Feb 19 '23 04:02

rciovati


your should fix the problom like this
i.setClassName("com.example.hello", "com.example.hello.LoginActivity")

like image 33
3h3 Avatar answered Feb 19 '23 05:02

3h3