AlarmManager 设置和取消定时器 实例

使用 AlarmManager 设置定时器的方法如下:

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                new Intent(ACTION_TIMEOUT), PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60 * 1000, // 60s later
                pendingIntent);

使用 AlarmManager 取消定时器的方法如下:

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                new Intent(ACTION_TIMEOUT), PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        try {
            am.cancel(pendingIntent);
            Log.d(TAG, "Alarm is Canceled.");
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "Alarm is not Canceled: " + e.toString());
        }

下面是完整的测试程序的代码的 java 部分

package com.toby.test_application;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final String TAG = "Test_MainActivity";
    private static final String ACTION_TIMEOUT = "com.toby.action.TIMEOUT";

    private static IntentFilter s_intentFilter;

    static {
        s_intentFilter = new IntentFilter();
        s_intentFilter.addAction(ACTION_TIMEOUT);
    }

    private final BroadcastReceiver m_testReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(ACTION_TIMEOUT)) {
                Log.d(TAG, "ACTION_TIMEOUT is Received");
                Toast.makeText(MainActivity.this,
                        TAG + " " + ACTION_TIMEOUT, Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerReceiver(m_testReceiver, s_intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(m_testReceiver);
    }

    public void startAlarm(View view) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                new Intent(ACTION_TIMEOUT), PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60 * 1000, // 60s later
                pendingIntent);

        Log.d(TAG, "Alarm is Set.");
    }

    public void stopAlarm(View view) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                new Intent(ACTION_TIMEOUT), PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        try {
            am.cancel(pendingIntent);
            Log.d(TAG, "Alarm is Canceled.");
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "Alarm is not Canceled: " + e.toString());
        }
    }
}

然后是布局文件 activity_main.xml:




    

String.xml 文件的代码:


    Test_Application

    Start Alarm
    Stop Alarm

程序的运行效果:

运行效果

你可能感兴趣的:(AlarmManager 设置和取消定时器 实例)