Start an activity when screen on
Asked Answered
J

1

6

I Have designed a lockscreen for adnroid. i'm trying to use a broadcast receiver for start the lockscreen activity when user pushes power button to unlock.

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
        Log.w("BAM", "Screen went on");
    }

    else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
        Log.w("BAM","Screen went off");
    }
}

}

android manifest:`

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyLockScreenActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name="MyAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/admin"/>

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".Receiver">
      <intent-filter>
        <action android:name="android.intent.action.USER_PRESENT" />
        <action android:name="android.intent.action.ACTION_SHUTDOWN" />
     </intent-filter>
    </receiver>
    <activity android:name=".LockScreen"></activity>
</application>

`

private ActionBar actionBar;
private ViewPager viewer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.lockscreen);
    actionBar = getSupportActionBar();
    actionBar.hide();

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new Receiver();
    registerReceiver(mReceiver, filter);

    viewer = (ViewPager) findViewById(R.id.viewr);
    viewer.setAdapter(new MyAdapter(getSupportFragmentManager()));
}

but when user pushes power button Lockscreen activity starts after 1-3 seconds. is it appropriate to start the activity when user pushes power button first to screen off? how can i do it?

thank you for your advice. (sorry for my bad English!)

Justiceship answered 29/4, 2015 at 14:4 Comment(1)
use: if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){ }Alyshaalysia
A
4

First, unlike other broad casted intents, for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON you CANNOT declare them in your Android Manifest! I’m not sure exactly why, but they must be registered in an IntentFilter in your JAVA code

use:

Edit again

public class YourActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YourLayout);

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    public class ScreenReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {

            if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
                Log.w("BAM", "Screen went on");
            }

            else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                Log.w("BAM","Screen went off");
            }
        }
    }
} 

Tested, Worked!

Alyshaalysia answered 29/4, 2015 at 14:13 Comment(9)
it doesn't work. activity doesn't start when screen is off. i even <action android:name="android.intent.action.SCREEN_OFF" /> in my manifest. Doesn't need it any permissions?Justiceship
Found the problem, read my editted post ;) , Tested this code my self and it worked.Alyshaalysia
i editted my post. check the codes. thank you for your advices.Justiceship
Remove <receiver android:name=".Receiver"> to </receiver> from your manifestAlyshaalysia
Read the yellow block in my postAlyshaalysia
Place your receiver code somewhere inside your activity codeAlyshaalysia
I have not to add anything to manifest file? (I have not to add my receiver to manifest?)Justiceship
Correct, receiver get added dynamicallyAlyshaalysia
Well, while this will work at first sight, it will not work when android decides to kill your application because of some random reasons. The lifetime of programmatically registered receivers is entangled with the lifetime of your application.Music

© 2022 - 2024 — McMap. All rights reserved.