设置报警管理是创建2个实例我的主要活动我的、实例

2023-09-12 22:42:59 作者:這點疼算什麽

我有2个活动,一个主要活动和SetAlarm活动。我叫SetAlarm活动的主。当我设置警报创建我的主要的一个实例。我如何设置警报,而无需创建主的另一个实例,还是我杀设立报警前的主?很新的这一点。我看了几个报警经理的例子,但他们似乎都设立了新的目标,我认为这是创造我的两个实例。这是你如何设置警报。它熄灭。

下面是我如何打电话从主SetAlarm:

 公共无效setAlarm(查看视图){
    意向意图=新的意图(这一点,SetAlarmActivity.class);
    startActivityForResult(意向,2);
}
 

下面是我如何设置报警:

 公共无效setUpAlarm(){
    如果(详细)Log.v(TAG,+++在setUpAlarm +++);
        PLAY_MUSIC =Y;
        意图int​​entAlarm =新的意图(这一点,MainActivity.class);
        intentAlarm.putExtra(playMusic,PLAY_MUSIC);
        intentAlarm.putExtra(MPOS,业务总);
        intentAlarm.putExtra(结果,ALARM_SET);
        的setResult(RESULT_OK,intentAlarm);
        pIntent = PendingIntent.getActivity(这一点,12345,
                intentAlarm,PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager上午=(AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
        am.set(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),pIntent);
    } // setAlarmPlaySong
 
Zabbix 监控平台设置报警发送邮件

我在主体割报警器关闭:

  @覆盖
公共无效onResume(){
    如果(详细)Log.v(TAG,+++在onResume +++);
    super.onResume();
    意向意图= getIntent()
    如果(intent.hasExtra(playMusic)及&安培; intent.hasExtra(MPOS)){
   playMusicFlag = intent.getStringExtra(playMusic);
   MPOS = intent.getIntExtra(MPOS,0);
   如果(playMusicFlag.equalsIgnoreCase(Y)){
       如果(详细)Log.v(TAG,+++ playMusicFlag是SET +++);
           playSongs();
               showStopAlarmButton();
       } // 如果
   }
}
 

解决方案

如果你想你的startActivity不应该开始阿拉姆活动的多个实例 你应该去你的清单和需要添加一个名为launchMode的属性为你报警的活动,并把它设置为SingleTop,这将确保只有一个实例保留在taskk后​​退堆栈(PLAC,每一个活性存在于后进先出的方式)

I have 2 activities, a Main Activity and SetAlarm Activity. I call SetAlarm Activity from Main. When I set up the alarm I create an instance of my main. How do I set up the alarm without creating another instance of the Main or do I kill the main before setting up the alarm? Quite new to this. I have read several of the alarm manager examples, but they all seem to set up a new intent and I think this is what is creating my 2 instances. Is this how you set up the alarm. It does go off.

Here is how I call SetAlarm from the Main:

public void setAlarm(View view) {
    Intent intent = new Intent(this, SetAlarmActivity.class);
    startActivityForResult(intent, 2);  
} 

Here is how I set up the Alarm:

public void setUpAlarm() {
    if (VERBOSE) Log.v(TAG, "+++ IN setUpAlarm +++");                   
        PLAY_MUSIC = "Y";
        Intent intentAlarm = new Intent(this, MainActivity.class);
        intentAlarm.putExtra("playMusic",PLAY_MUSIC);
        intentAlarm.putExtra("mPos", mPos);
        intentAlarm.putExtra("result",ALARM_SET);
        setResult(RESULT_OK,intentAlarm);
        pIntent = PendingIntent.getActivity(this, 12345, 
                intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
        am.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pIntent );
    } // setAlarmPlaySong

I cut the alarm off in the main:

@Override
public void onResume() {
    if (VERBOSE) Log.v(TAG, "+++ IN onResume +++");
    super.onResume();  
    Intent intent = getIntent()
    if (intent.hasExtra("playMusic")  && intent.hasExtra("mPos")) {
   playMusicFlag = intent.getStringExtra("playMusic"); 
   mPos = intent.getIntExtra("mPos", 0);   
   if (playMusicFlag.equalsIgnoreCase("Y")) {
       if (VERBOSE) Log.v(TAG, "+++ playMusicFlag is SET+++");
           playSongs();
               showStopAlarmButton();
       } // if    
   }
}

解决方案

if you want that your startActivity should not start multiple instances of alam activity you should go to your manifest and have to add an attribute named launchMode for your alarm activity and set it to SingleTop that will ensure that only one instance remains in the taskk back stack(plac where every activity resides in LIFO manner)

 
精彩推荐
图片推荐