AlarmManager没有调用任务?

时间:2017-07-07 06:13:43

标签: android broadcastreceiver alarmmanager android-broadcastreceiver

我想在我的应用程序中每天晚上10点之后执行自动注销,因为我已经实现了一个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");


    }
}

我的清单文件(我已添加以下权限):

<?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>

2 个答案:

答案 0 :(得分:0)

Calendar对象中更改并设置 10 PM 的正确时间:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,21);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,59);

答案 1 :(得分:0)

你放了

<receiver android:name=".AutoLogoutRecevier" />

在你的清单中