Android闹钟-AlarmManager
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 { // 声明Button private Button setBtn, cancelBtn; // 定义广播Action private static final String BC_ACTION = "com.action.BC_ACTION"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置当前布局视图 setContentView(R.layout.main); // 实例化Button setBtn = (Button) findViewById(R.id.Button01); cancelBtn = (Button) findViewById(R.id.Button02); // 获得AlarmManager实例 final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); // 实例化Intent Intent intent = new Intent(); // 设置Intent action属性 intent.setAction(BC_ACTION); intent.putExtra("msg", "你该去开会啦!"); // 实例化PendingIntent final PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); // 获得系统时间 final long time = System.currentTimeMillis(); // 设置按钮单击事件 setBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 重复提示,从当前时间开始,间隔5秒 am.setRepeating(AlarmManager.RTC_WAKEUP, time, 5 * 1000, pi); } }); // 设置按钮单击事件 cancelBtn.setOnClickListener(new OnClickListener() { @Override public 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 beFLAG_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 if
FLAG_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 { @Override public void onReceive(Context context, Intent intent) { // 获得提示信息 String msg = intent.getStringExtra("msg"); // 显示提示信息 Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); } }
<receiver android:name="MyReceiver"> <intent-filter> <action android:name="com.action.BC_ACTION"/> </intent-filter> </receiver>结果:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。