android开机自启广播无效果的曲线解决方案

阅读更多
AndroidManifest.xml



    
    
    
        
            
                
                
            
        
        
            
                
                
            
        
        
    



MyService
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;
    }
}


BootBroadcastReceiver
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);
        }
    }

}


如代码所示,Intent.ACTION_BOOT_COMPLETED表示boot启动完毕后,系统会发送一个广播过来,理论上确实可以接收到,大部分手机估计也都是可以的,但是就是有那么些手机rom被改得不可思议,非要加上个android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY才可以在开机的时候让系统执行Intent.ACTION_BOOT_COMPLETED广播的发送,根据LOG可以看出,以OPPO1107为例,系统是先发送了ACTION_AUDIO_BECOMING_NOISY后发送了ACTION_BOOT_COMPLETED,仅以此博,做个纪念

你可能感兴趣的:(android)