来自Broadcastreceiver的呼叫服务未按预期工作

时间:2017-03-17 09:10:40

标签: java android alarmmanager

我希望我的服务在警报管理器指定的时间运行。但在我的情况下,它运行在我指定的时间,如果我在指定的时间服务运行后打开应用程序。我希望它只在指定的时间运行,它应该每天重复。我不知道我写的代码有什么问题。任何帮助表示赞赏。

这是我的服务类:

public class AutoLogout extends Service {

private SQLiteHandler db;
private SessionManager session;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // SqLite database handler
    db = new SQLiteHandler(getApplicationContext());
    // session manager
    session = new SessionManager(getApplicationContext());
    logoutUser();
   Toast.makeText(getApplicationContext(),"Service started",Toast.LENGTH_SHORT).show();
    return Service.START_STICKY;
}

/**
 * Logging out the user. Will set isLoggedIn flag to false in shared
 * preferences Clears the user data from sqlite users table
 */
private void logoutUser() {
    session.setLogin(false);
    db.deleteData();
    // Launching the login activity
    Intent intent = new Intent(this, LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    stopService(new Intent(getApplicationContext(),AutoLogout.class));
    Toast.makeText(getApplicationContext(),"Service stopped",Toast.LENGTH_SHORT).show();
}
}

这是我的BroadcastReciever类:

public class AlarmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "MyReceiver Started", Toast.LENGTH_SHORT).show();
        Intent intent1 = new Intent(context,AutoLogout.class);
        context.startService(intent1);
    }
}

我在调用oncreate方法调用警报管理器的CategoryActivity.java:

public class CategoryActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category);
    Calendar cal_alarm = Calendar.getInstance();
            cal_alarm.set(Calendar.HOUR_OF_DAY,14);
            cal_alarm.set(Calendar.MINUTE,46);
            cal_alarm.set(Calendar.SECOND,00);
            Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis() , pendingIntent);

}
}

这是我的清单文件:

    <service android:name=".services.AutoLogout" />
    <receiver android:name=".services.AlarmBroadcastReceiver"/>

1 个答案:

答案 0 :(得分:1)

问题正在发生,因为每次打开设置闹钟的活动时,它都会重新初始化闹钟 - 但是,每次打开活动时闹钟都会错误地触发,因为正确的闹钟时间(14: 46:00)可能已经在当天通过了,当你在已经过去的时间内设置0 0 1 290 2 450 dtype: int64 警报时,它会立即触发。

为了解决这个问题,我们需要检查当天是否已经过了闹钟时间,如果有,请在以下天将闹钟设置为14:46:00 :

AlarmManager

以下是if (cal_alarm.getTimeInMillis() < System.currentTimeMillis()) { cal_alarm.add(Calendar.DATE, 1); } 的内容:

CategoryActivity

public class CategoryActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_category); Calendar cal_alarm = Calendar.getInstance(); cal_alarm.setTimeInMillis(System.currentTimeMillis()); // i added this to ensure the calendar is being configured correctly cal_alarm.set(Calendar.HOUR_OF_DAY,14); cal_alarm.set(Calendar.MINUTE,46); cal_alarm.set(Calendar.SECOND,00); //check if time has already passed today, adjust alarm to tomorrow if it has if (cal_alarm.getTimeInMillis() < System.currentTimeMillis()) { cal_alarm.add(Calendar.DATE, 1); } Intent intent = new Intent(this, AlarmBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_ONE_SHOT); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis() , pendingIntent); } } 语句只检查当前时间是否大于if设置的时间,如果是,则设置闹钟为14:46:00 明天< / em>的。如果您需要更多澄清,请随时询问!