Android闹钟-AlarmManager

来源:互联网 发布:没有女朋友知乎 编辑:程序博客网 时间:2024/05/16 11:43

AlarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服务。AlarmManager对象一般通过Context.getSystemService(Context.ALARM_SERVICE)方法获得。


下面看一个例子加深理解:

package com.app;import com.app.R;import android.app.Activity;import android.app.AlarmManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** *  * 测试AlarmManager */public class MainActivity extends Activity {// 声明Buttonprivate Button setBtn, cancelBtn;// 定义广播Actionprivate static final String BC_ACTION = "com.action.BC_ACTION";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置当前布局视图setContentView(R.layout.main);// 实例化ButtonsetBtn = (Button) findViewById(R.id.Button01);cancelBtn = (Button) findViewById(R.id.Button02);// 获得AlarmManager实例final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);// 实例化IntentIntent intent = new Intent();// 设置Intent action属性intent.setAction(BC_ACTION);intent.putExtra("msg", "你该去开会啦!");// 实例化PendingIntentfinal PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,intent, 0);// 获得系统时间final long time = System.currentTimeMillis();// 设置按钮单击事件setBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 重复提示,从当前时间开始,间隔5秒am.setRepeating(AlarmManager.RTC_WAKEUP, time,5 * 1000, pi);}});// 设置按钮单击事件cancelBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {am.cancel(pi);}});}}

注:PendingIntent的一个静态方法,

getBroadcast

public static PendingIntent getBroadcast(Context context,                                         int requestCode,                                         Intent intent,                                         int flags)
Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().

参数:
context - The Context in which this PendingIntent should perform the broadcast.
requestCode - Private request code for the sender (currently not used).
intent - The Intent to be broadcast.
flags - May be FLAG_ONE_SHOT, FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
返回:
Returns an existing or new PendingIntent matching the given parameters. May return null only ifFLAG_NO_CREATE has been supplied.


package com.app;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// 获得提示信息String msg = intent.getStringExtra("msg");// 显示提示信息Toast.makeText(context, msg, Toast.LENGTH_LONG).show();}}


别忘了在AndroidManifest.xml中注册MyReceiver:
<receiver android:name="MyReceiver">        <intent-filter>        <action android:name="com.action.BC_ACTION"/>        </intent-filter> </receiver>
结果:



2 0
原创粉丝点击