Android broadcast receiver not working when trying

2019-06-11 08:19发布

So I am trying to develop a custom lockscreen

but my broadcastreceiver won't fire

my manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.alexander.fuchs.lockscreen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".app"
            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=".myreceiver">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_OFF"/>
                <action android:name="android.intent.action.SCREEN_ON"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

my receiver :

public class myreceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("receiver","works");
        Toast.makeText(context,"works",Toast.LENGTH_LONG).show();
        Intent in=new Intent(context,app.class);
         context.startActivity(in);
    }
}

the receiver should show me that it is fired :D but ther aren't any logs in logcat

1条回答
\"骚年 ilove
2楼-- · 2019-06-11 08:48

well , for the screen off and screen on , this cannot be inside the manifest , but only at runtime . see this: http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

for the bootup , it must be in the manifest , so something else is wrong with it.check the path of the class. this is surely the cause of the problem.

查看更多
登录 后发表回答