
MainAcitivity
public class MainActivity extends Activity { private PendingIntent pendingIntent; private static Context context; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Intent myIntent = new Intent(this,MyReceiver.class); pendingIntent = PendingIntent.getbroadcast(MainActivity.this,myIntent,0); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),12 * 60 * 60,pendingIntent); } //end onCreate} MyReceiver
public class MyReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context,Intent intent) { Intent service1 = new Intent(context,MyAlarmService.class); context.startService(service1); } } MyAlarmService
public class MyAlarmService extends Service { private notificationmanager mManager; @OverrIDe public IBinder onBind(Intent arg0) { // Todo auto-generated method stub return null; } @OverrIDe public voID onCreate() { // Todo auto-generated method stub super.onCreate(); } @SuppressWarnings("static-access") @OverrIDe public voID onStart(Intent intent,int startID) { super.onStart(intent,startID); mManager = (notificationmanager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class); Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!",System.currentTimeMillis()); intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_top| Intent.FLAG_ACTIVITY_CLEAR_top); PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),intent1,PendingIntent.FLAG_UPDATE_CURRENT); notification.flags |= Notification.FLAG_auto_CANCEL; notification.setLatestEventInfo(this.getApplicationContext(),"AlarmManagerDemo",pendingNotificationIntent); mManager.notify(0,notification); } @OverrIDe public voID onDestroy() { // Todo auto-generated method stub super.onDestroy(); }} NextActivity
public class NextActivity extends Activity{ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Intent intentt = new Intent(this,MyReceiver.class); PendingIntent pintent = PendingIntent.getbroadcast(this,10,intentt,0); AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); stopService(intentt); alarm.cancel(pintent); } } 表现
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.example" androID:versionCode="1" androID:versionname="1.0" > <uses-sdk androID:minSdkVersion="8" androID:targetSdkVersion="10" /> <uses-permission androID:name="androID.permission.WAKE_LOCK" /> <application androID:allowBackup="true" androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name="com.example.MainActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name="com.example.NextActivity" androID:label="Next Screen"> </activity> <service androID:name=".MyAlarmService" androID:enabled="true" /> <receiver androID:name=".MyReceiver"/> </application></manifest>
它仍在发出警报,执行此 *** 作后无法停止/取消.
解决方法 创建警报时,使用此PendingIntent:Intent myIntent = new Intent(MainActivity.getAppContext(),MyReceiver.class);pendingIntent = PendingIntent.getbroadcast(MainActivity.this,0);
当您尝试取消它时,您正在使用此PendingIntent:
Intent intentt = new Intent(MainActivity.getAppContext(),NextActivity.class);PendingIntent pintent = PendingIntent.getService(this,0);
要取消警报时,需要使用相同的代码创建PendingIntent.使用getbroadcast()而不是getService()并使用MyReceiver.class而不是NextActivity.class.
此外,您无需在MainActivity中保存应用程序上下文.你可以删除所有这些静态的东西.在执行新的Intent(Context,Thing.class)时,您可以将它用作Context参数
总结以上是内存溢出为你收集整理的android – 如何在另一个活动中停止/取消警报管理器?全部内容,希望文章能够帮你解决android – 如何在另一个活动中停止/取消警报管理器?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)