Диспетчер аварийных сигналов не перепланируется после перезагрузки даже при добавлении при получении - PullRequest
1 голос
/ 13 февраля 2020

Я зарегистрировал мой получатель в манифесте с фильтром намерений (action.BOOT_COMPLETED), но после перезагрузки мой сервис вызвал только один раз и больше никогда не звонил. Не могу понять, в чем проблема.

вот мой код

основная деятельность

public class MainActivity extends AppCompatActivity {

Button startButton,stopButton;
static AlarmManager alarm;
static PendingIntent pIntent;
EditText numberText;
TextView getText;
static SharedPreferences pref;
int x;


@SuppressLint("WrongConstant")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startButton=findViewById(R.id.startButton);
    stopButton=findViewById(R.id.stopButton);
    numberText=findViewById(R.id.numberText);
    getText=findViewById(R.id.getText);
    pref = PreferenceManager.getDefaultSharedPreferences(this);
    scheduleAlarm();

    Intent in= new Intent(this,MyServices.class);
    this.startService(in);

}
public  void scheduleAlarm() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    Intent intent = new Intent(this, MyAlarm.class);

    long firstMillis = System.currentTimeMillis(); // alarm is set right away

    alarm = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);

    pIntent = PendingIntent.getBroadcast(this, MyAlarm.REQUEST_CODE,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);

    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),5000, pIntent);


}

public void start(View view) {

    SharedPreferences.Editor editor=pref.edit();
    editor.putInt("number", Integer.parseInt(numberText.getText().toString()));
    editor.commit();


}

public void stop(View view) {
    stopService(new Intent(this,MyServices.class));
    alarm.cancel(pIntent);
}
}

мой приемник

public class MyAlarm extends BroadcastReceiver {

public static final int REQUEST_CODE = 1;
Calendar calendar;

@Override
public void onReceive(Context context, Intent intent) {

        Intent i = new Intent(context, MyServices.class);

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
            calendar = Calendar.getInstance();

            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 7);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            Intent in = new Intent(context, MyServices.class);

            long firstMillis = System.currentTimeMillis(); // alarm is set right away

            alarm = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);

            pIntent = PendingIntent.getBroadcast(context, MyAlarm.REQUEST_CODE,
                    intent, PendingIntent.FLAG_CANCEL_CURRENT);

            alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),5000, pIntent);
        }
        context.startService(i);
        }
}

мой сервис

public class MyServices extends IntentService {

public MyServices() {
    super("MyServices");
}




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

if (MainActivity.pref.getInt("number", x) == 5) {
    Notification.Builder notification = new Notification.Builder(this)
            // this Builder class is deprecated
            .setDefaults(NotificationCompat.DEFAULT_SOUND)
            .setPriority(Notification.PRIORITY_MAX)
            .setLights(0xff00ff00, 300, 100)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("My notification")
            .setContentText("Hello Peter");


    NotificationManagerCompat manager = NotificationManagerCompat.from(this);

    manager.notify(0, notification.build());

    return START_STICKY;
}

@Override
protected void onHandleIntent( Intent intent) {

}


}

манифест

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".MyAlarm"
        android:process=":remote" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service android:name=".MyServices"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:exported="false"
        ></service>
</application>

.............................................. .... ..........................................

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...