android开机自启广播无效果的曲线解决方案
- - 移动开发 - ITeye博客已有 0 人发表留言,猛击->> 这里<<-参与讨论. —软件人才免语言低担保 赴美带薪读研.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="where.com.whereareyou"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-sdk android:minSdkVersion="7" android:sharedUserId="android.uid.system" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Holo.NoActionBar"> <activity android:name="where.com.whereareyou.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="where.com.whereareyou.BootBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.media.AUDIO_BECOMING_NOISY" /> </intent-filter> </receiver> <service android:name=".MyService"/> </application> </manifest>
package where.com.whereareyou; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; /** * Created by Administrator on 2015-10-4. */ public class MyService extends Service { public final static String TAG = "MyService"; @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG,"onStartCommand"); return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.e(TAG,"onDestroy"); } @Override public IBinder onBind(Intent intent) { return null; } }
package where.com.whereareyou; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * Created by Administrator on 2015-10-4. */ public class BootBroadcastReceiver extends BroadcastReceiver { static final String TAG = "BootBroadcastReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.e(TAG, "BootBroadcastReceiver onReceive"); if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) { Log.e(TAG, "action=" + intent.getAction()); Intent ootStartIntent = new Intent(context, MyService.class); context.startService(ootStartIntent); } } }