Boot/ScreenOn Broadcast Receiver not working

后端 未结 2 812
我在风中等你
我在风中等你 2021-01-06 00:14

I have a blank HelloWorld Application:

package tutorials.TestReceivers;

import android.app.Activity;
import android.os.Bundle;

public class TestReceiversAc         


        
相关标签:
2条回答
  • 2021-01-06 00:27

    Delete android-permission="android.permission.RECEIVE_BOOT_COMPLETED". Add a <uses-permission> element for this permission as a child of the <manifest> element.

    If the problems continue, use adb logcat, DDMS, or the DDMS perspective in Eclipse to look at LogCat and examine the stack trace associated with your crash.

    Here is a sample project showing how to get control at boot time.

    SCREEN_ON will not work from the manifest.

    0 讨论(0)
  • 2021-01-06 00:31

    After a long time of frustration, I solved the problem above.

    The right way to register Boot Broadcast Receiver (and open activity according to it), is:

    Blank HelloWorld Application (TestReceiversActivity.java):

    package tutorials.TestReceivers;
    
    import android.app.Activity;
    public class TestReceiversActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }
    

    Another Boot Receiver Class (BootReceiver.java)

    package tutorials.TestReceivers;
    
    import android.content.BroadcastReceiver;
    public class BootReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
             Log.d("DAVID", "Hi, Boot reciver was catch!");
             Intent i = new Intent(context, TestReceiversActivity.class);
             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             context.startActivity(i);
           }
    }
    

    Note: You must set the flag to make it work!

    Set the manifest to:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="tutorials.TestReceivers"
          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/icon" android:label="@string/app_name">
                <receiver android:name=".BootReceiver" >
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
            <activity android:name=".TestReceiversActivity"
                      android:label="@string/app_name">
                    <intent-filter >
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
            </activity>
        </application>
     </manifest>
    

    Enjoy!

    0 讨论(0)
提交回复
热议问题