Boot Receiver not working

有些话、适合烂在心里 提交于 2019-12-17 18:42:21

问题


Manifest:

         <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".AlarmActivity"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                </intent-filter>
            </activity>
            <receiver android:name="CallReciver">
                <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE">  

</action>
                </intent-filter>
            </receiver>
            <receiver android:name=".SmsReceiver"> 
               <intent-filter android:priority="1000">
                    <action android:name=
                        "android.provider.Telephony.SMS_RECEIVED" /> 
                </intent-filter> 
            </receiver>
             <receiver android:name=".OnBootReceiver">
          <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
          </intent-filter>
        </receiver>
            <service
                android:enabled="true"
                android:name=".AlarmService">
            </service>
        </application>
         <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
         </uses-permission>
        <uses-permission android:name="android.permission.READ_PHONE_STATE">
        </uses-permission>
        <uses-permission android:name="android.permission.WRITE_SMS">
        </uses-permission>
       <uses-permission android:name="android.permission.READ_SMS">
        </uses-permission>
       <uses-permission android:name="android.permission.SEND_SMS">
        </uses-permission>
        <uses-permission android:name="android.permission.RECEIVE_SMS">
        </uses-permission>
        <uses-permission android:name="android.permission.INTERNET">
        </uses-permission>

Receiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class OnBootReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d("Test","booot");
        Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
    }
}

Receiver doesn't work. I turn off and on my device and nothing happens. SMS And Call Receiver in this project work good. SMS Receiver and CallReceviver - works good. First post updated - added full manifest.


回答1:


If you have HTC device you also need to register for "android.intent.action.QUICKBOOT_POWERON". So the entry in manifest should be:

    <receiver android:name=".OnBootReceiver"> 
        <intent-filter> 
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter> 
    </receiver>    

On my HTC, if I turn off the device and turn it on for a while I got QUICKBOOT_POWERON and no BOOT_COMPLETED.

If I turn off the device and remove the battery for a while - I got BOOT_COMPLETED after start.




回答2:


Put permission

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



回答3:


Also know that in Android >= 3.1 the app gets installed in 'stopped' state and will not get boot and shutdown events until the user 'does something' with the app at least once. See this post on the topic.




回答4:


Try this::

   <receiver android:enabled="true" android:exported="true"
        android:name=".OnBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

Cheers...!!!



来源:https://stackoverflow.com/questions/7978403/boot-receiver-not-working

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