Boot Complete广播接收器

时间:2015-09-07 14:07:56

标签: android broadcastreceiver

我正在开发一个应用程序,它通过警报管理器提供每日通知,并且它运行良好但我在设备重启时遇到问题我已经构建了Boot_Complete广播接收器,但是我希望此广播检查服务是否正常它在重启之前运行或未运行。 这是我的MainActivity.java

public void startMorning()
{
    AlarmManager morningAlarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent morningIntent = new Intent(AutoAzkarSetting.this,MorningReciver.class);
    morningIntent.putExtra("numCheck",1);
    moPending = PendingIntent.getBroadcast(AutoAzkarSetting.this, 0, morningIntent, 0);
    Calendar morningCalender = Calendar.getInstance();
    morningCalender.setTimeInMillis(System.currentTimeMillis());
    morningCalender.set(Calendar.HOUR_OF_DAY, 8);
    morningAlarm.setRepeating(AlarmManager.RTC_WAKEUP,morningCalender.getTimeInMillis()+12*60*60*1000,AlarmManager.INTERVAL_DAY,moPending);
    Toast.makeText(this,"Morning Alarm Set",Toast.LENGTH_LONG).show();
}

这是MorningReceiver.java

public class MorningReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

        Intent morIntent = new Intent(context, MorningService.class);
        context.startService(morIntent);
}
}

这是MorningService.java

public class MorningService extends Service {

WindowManager windowManager ;
WindowManager.LayoutParams params , params2;
ImageView azkarHead , closeHead;
int morId ;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

        String morningContent = this.getResources().getString(R.string.notyContent);
        String morningSub = this.getResources().getString(R.string.moevSub);
        Uri MorningSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        final NotificationManager morningManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification morningNotification = new NotificationCompat.Builder(this)
                .setContentTitle("Morning")
                .setContentText(morningContent)
                .setSubText(morningSub)
                .setSmallIcon(R.drawable.ic_launcher)
                .setSound(MorningSound)
                .setAutoCancel(false)
                .build();
        morningManager.notify(morId, morningNotification);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        azkarHead = new ImageView(this);
        azkarHead.setImageResource(R.drawable.ic_launcher);
        params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.RIGHT;
        params.x = 0;
        params.y = 150;
        windowManager.addView(azkarHead, params);
        azkarHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent morningIndex = new Intent(getBaseContext(), AzkarList.class);
                morningIndex.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                morningIndex.putExtra("azkarHeadIndex", 0);
                startActivity(morningIndex);
                windowManager.removeView(azkarHead);
                windowManager.removeView(closeHead);
                morningManager.cancel(morId);
            }
        });

        closeHead = new ImageView(this);
        closeHead.setImageResource(R.drawable.ic_clear_white);
        params2 = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        params2.gravity = Gravity.TOP | Gravity.RIGHT;
        params2.x = 10;
        params2.y = 260;
        windowManager.addView(closeHead, params2);
        closeHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowManager.removeView(azkarHead);
                windowManager.removeView(closeHead);
                morningManager.cancel(morId);
            }
        });
        startForeground(morId, morningNotification);

    return START_STICKY;
}

所以有任何想法怎么做?????

1 个答案:

答案 0 :(得分:0)

如果服务是在共享首选项中启动或停止的,您可以存储,如下所示:

@Override
public void onCreate() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.edit().putBoolean("ServiceRunning", true).apply();
}

@Override
public void onDestroy() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.edit().putBoolean("ServiceRunning", false).apply();
}

然后,如果服务运行如下,您可以检查启动完成的接收器:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean("ServiceRunning", false))
    ...

请注意,仅当您在单个进程中运行应用程序时(默认情况下),此方法才有效。