我在应用程序上工作,此应用程序在通知栏上显示每个时段的通知,所以我想在调用广播接收器时显示此通知检查现在是否使用了应用程序(我的意思是应用程序正在运行,用户现在打开it)不要在通知栏上显示通知,请帮帮我。
This is the set of alarm in oncreate:
Intent iAlarm=new Intent(HomePage.this, AlarmReciever.class);
AlarmMan alarm=new AlarmMan();
alarm.SetAlarm(HomePage.this,System.currentTimeMillis() , 60000, true, 7896) ;
and this is onstop and on resume:
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addDataScheme("package");
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
AlarmReciever reciever1=new AlarmReciever();
this.registerReceiver(reciever1,filter);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
try{
AlarmReciever reciever2=new AlarmReciever();
this.unregisterReceiver(reciever2);
}catch(Error e)
{
Log.v("error in resume", e.getMessage());
}
}
This the setalarm function:
public static void SetAlarm (Context c , long time , int period ,boolean repeat ,int id ){
AlarmManager al = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(c,AlarmReciever.class);
PendingIntent pi = PendingIntent.getBroadcast(c,7896,i,PendingIntent.FLAG_UPDATE_CURRENT);
if(repeat ){
al.setRepeating(AlarmManager.RTC_WAKEUP,time,period,pi);
}else {
al.set(AlarmManager.RTC_WAKEUP, time, pi);
}
}
答案 0 :(得分:0)
退出应用时,只需停止广播接收器:
@Override
public void onStop()
{
super.onStop();
something.unregisterListener(this);
}
这样,广播监听器不会发送任何通知,而应用程序关闭。当用户启动应用程序时,您再次打开广播接收器。
修改强> 这是onCreate():
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addDataScheme("package");
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
registerReceiver(newPackageReceiver, filter); // This is how you register the receiver.
这是接收器的功能:这是在onCreate()之外:
private final BroadcastReceiver newPackageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "New App Installed!", Toast.LENGTH_SHORT).show();
}
};
因此,当我想注册接收器时,我喜欢这样:
registerReceiver(newPackageReceiver, filter);
当我想关掉它时:
unregisterReceiver(newPackageReceiver);
所以你应该在onStop()方法中启动接收器,并在onCreate()中停止它,也许onResume(); 。如果这有帮助,请竖起大拇指。