我想每天晚上 10 点后在我的应用程序中执行自动注销,为此我已经实现了 AlarmManager 任务,但它没有调用 .出于测试目的,我给出了其他时间。(例如
calendar.set(Calendar.HOUR_OF_DAY,15);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,30);
( 警报管理器代码:
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");
}
}
我的清单文件(我添加了以下权限(:
<?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>
在Calendar
对象中更改并设置正确的晚上10 点时间:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,21);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,59);
你把吗
<receiver android:name=".AutoLogoutRecevier" />
在您的清单中?