问题
I want to execute a auto-logout after 10 PM everyday in my application, for that that I have implemented a AlarmManager task but it is not invoking .For testing purpose I have given the other timing .(e.g
calendar.set(Calendar.HOUR_OF_DAY,15);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,30);
) AlarmManager Code:
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);
My broadcast receiver class :
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");
}
}
My manifest file (I have added the following permission) :
<?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>
回答1:
Change and set right time for 10 PM in your Calendar
object:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,21);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,59);
回答2:
Did you put
<receiver android:name=".AutoLogoutRecevier" />
in your manifest?
来源:https://stackoverflow.com/questions/44963723/alarmmanager-not-invoking-the-task