質問

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)

他のヒント

In default, an Activity can be instantiated multiple times on multiple tasks. If you want to keep it single, specify android:launchMode="singleTask" on the activity declaration in AnroidManifest.xml and override Activity#onNewIntent() on your main activity to receive a new intent from AlarmManager if main activity is already instantiated.

See Tasks and Back Stack to learn more. You are facing almost same situation shown in Figure 3.

I dont know why you have the SetAlarm Activity, you dont need a activity to set the alarm. Anyways, AlarmManager is a pain to get working. It took me a while to get it up and running. This is what I have in my code now, running.

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 5);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent notifyintent = new Intent(this, OnAlarmReceiver.class);
        notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyintent.setAction("android.intent.action.NOTIFY");
        PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000,
                notifysender);

OnAlarmReceiver

public class OnAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // PullPendingRequests.acquireStaticLock(context)
        try {
            lock = getLock(context);
            lock.acquire();
            context.startService(new Intent(context, UpdateCustomerRequests.class));
        } finally {
            if (lock.isHeld()) {
                lock.release();
            }
        }
    }

    private static final String NAME = "com.commonsware.cwac.wakeful.WakefulIntentService";
    private static volatile PowerManager.WakeLock lockStatic = null;
    private static PowerManager.WakeLock lock;

    synchronized private static PowerManager.WakeLock getLock(Context context) {
        if (lockStatic == null) {
            PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

            lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
            lockStatic.setReferenceCounted(true);
        }

        return (lockStatic);
    }
}

IntentService which is called by OnAlarmReceiver

public class UpdateCustomerRequests extends IntentService {
@Override
    final protected void onHandleIntent(Intent intent) {
        //
        //Your stuff here
        //
    }

    public class LocalBinder extends Binder {
        public UpdateCustomerRequests getService() {
            return UpdateCustomerRequests.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return bindToHomeScreen;
    }
}

Android Manifest

  • Inside manifest tag

<uses-permission android:name="android.permission.WAKE_LOCK" />

  • Inside application tag

        <receiver
            android:name="com.taxeeta.support.OnAlarmReceiver"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.NOTIFY" />
            </intent-filter>
        </receiver>
    
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top