Android broadcast receiver not working when trying to receive bootcomplete or screen off

戏子无情 提交于 2020-01-25 20:31:09

问题


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:


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.



来源:https://stackoverflow.com/questions/10770361/android-broadcast-receiver-not-working-when-trying-to-receive-bootcomplete-or-sc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!