1. 在AndroidManifest.xml定义相关权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
2. 在Main Activity中建立AlarmManager:
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, Schedule_Time, pendingIntent);
3. 在Receiver中建立响应:
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, ActionActivity.class);
Bundle bundleRet = new Bundle();
bundleRet.putString("STR_CALLER", "");
i.putExtras(bundleRet);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
没有评论:
发表评论