我有2个活动,一个主要活动和一个SetAlarm活动。我从Main调用SetAlarm Activity。当我设置闹钟时,我创建了一个主要的实例。如何在不创建Main的另一个实例的情况下设置警报,还是在设置警报之前杀死main?对此非常新。我已经阅读了几个警报管理器示例,但它们似乎都设置了一个新的意图,我认为这就是创建我的2个实例。这是你如何设置闹钟。它确实发生了。
以下是我从Main调用SetAlarm的方法:
public void setAlarm(View view) {
Intent intent = new Intent(this, SetAlarmActivity.class);
startActivityForResult(intent, 2);
}
以下是我设置闹钟的方法:
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
我在主要部门切断了警报:
@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
}
}
答案 0 :(得分:2)
如果你想要你的startActivity不应该启动多个alam活动的实例 你应该去你的清单,并且必须为你的警报活动添加一个名为launchMode的属性,并将其设置为SingleTop,以确保只有一个实例保留在taskk后台堆栈中(每个活动以LIFO方式驻留的plac)
答案 1 :(得分:0)
默认情况下,Activity
可以在多个任务上多次实例化。如果要保持单一,请在AnroidManifest.xml中的活动声明中指定android:launchMode="singleTask"
并覆盖主活动上的Activity#onNewIntent()
,以便在主要活动已实例化时从AlarmManager
接收新意图
请参阅Tasks and Back Stack了解详情。您正面临图3 中显示的几乎相同的情况。
答案 2 :(得分:0)
我不知道为什么你有SetAlarm Activity,你不需要活动来设置闹钟。无论如何,AlarmManager很难开始工作。我花了一段时间才开始运行。这就是我现在在代码中运行的内容。
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);
}
}
OnAlarmReceiver调用的IntentService
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
<uses-permission android:name="android.permission.WAKE_LOCK" />
内部应用程序标记
<receiver
android:name="com.taxeeta.support.OnAlarmReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>