我想在我的應用程序來執行日常晚上10點後自動註銷,因爲這是我實現了一個AlarmManager任務,但它不調用。對於測試的目的,我已經給了其他時間。(如AlarmManager不調用任務?
calendar.set(Calendar.HOUR_OF_DAY,15);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,30);
) AlarmManager代碼:
sign_in_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboard(LoginActivity.this);
//implementation of auto logout
checkSuiteSession = new CheckSuiteSession(getApplicationContext());
checkSuiteSession.check_attendance_flag("true");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,15);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,30);
Intent autoLogoutIntent = new Intent(getApplicationContext(),AutoLogoutRecevier.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(),100,autoLogoutIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
我的廣播接收器類:
public class AutoLogoutRecevier extends BroadcastReceiver {
CheckSuiteSession checkSuiteSession;
@Override
public void onReceive(Context context, Intent intent) {
checkSuiteSession = new CheckSuiteSession(context);
//calling logout function
checkSuiteSession.logoutUser();
Log.d("LOGOUT","auto logout executed");
checkSuiteSession.check_attendance_flag("false");
}
}
我manif在Calendar
對象
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fresherguru.checksuite">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".fcm.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<receiver android:name=".autoLogoutReceiver.AutoLogoutRecevier">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".autoLogoutReceiver.ActivateLogin">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".autoLogoutReceiver.ChangeDialogStatus"
android:process=":remote"/>
<receiver android:name=".autoLogoutReceiver.ScreenUnlockReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<!--<action android:name="android.intent.action.ACTION_SHUTDOWN" />-->
</intent-filter>
</receiver>
<receiver android:name=".autoLogoutReceiver.DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
<service android:name=".service.CallHandleAlarmService"
android:enabled="true"/>
<activity
android:name=".home.LoginActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".home.HomeActivity"
android:theme="@style/AppTheme.NoActionBar" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".home.CheckSuiteForm"
/>
</application>
</manifest>
發佈您的清單。 –
@DavidWasser我已經添加了完整的清單,請檢查! – Sidhartha
清單看起來沒問題。嘗試使用'AlarmManager.set()'或'setExact()'而不是'setRepeating()'來查看是否可以讓警報觸發至少一次。此外,添加日誌記錄並輸出用於鬧鐘時間的毫秒數,並檢查該值是否與您預期的一樣。 –