1.问题
在测试Android开启广播接受的时候,Android3.0之后就增加了权限,在模拟器测试的时候是可以接受到广播并且开启的指定的App。不过在真机上面调试的时候,一直不能接受到,开机广播不能接受,网上也看了很多文章,没有解决。
2.解决
我的手机是Android4.0的,真机调试时也按网上解决方法试了,增加权限,安装之后先运行了一次,设置自启动项。不过都没有用。原来是我安装App都是安装在SDCard上面的,刚开机时要加载SDCard,可能加载完之后App就无法就收到开机广播了,最后我把App移至系统空间才解决
public class BootReceiver extends BroadcastReceiver { private static final String TAG = "BootReceiver"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "广播已经接受"); Intent activityIntent = new Intent(context, MainActivity.class); activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(activityIntent); } }
<!-- 权限是3.0之后要添加的 --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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="com.pt.receiver.BootReceiver"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application>