当我开始重复闹钟时,下面的代码运行正常。然后,我按下移动设备的主屏幕按钮以显示主屏幕。接下来,我长按主屏幕按钮并从最近的应用程序列表中恢复AlarmManager应用程序。 AlarmManager应用程序恢复后,重复警报停止。但是,我需要重复闹钟继续。
需要使用哪些代码来防止重复警报在恢复时停止?
我仍然是Android编程的初学者。非常感谢您的帮助和建议。
//The Activity Code:
//==================
public class AlarmManagerActivity extends Activity {
private AlarmManager alarmManager;
private BroadcastReceiver alarmReceiver;
private PendingIntent pendingIntent;
private int alarmCounter;
private EditText alarmsReceived;
private Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 10:
alarmsReceived.setText(String.valueOf(alarmCounter));
try {
Uri soundNotification = RingtoneManager.
getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringTone = RingtoneManager.
getRingtone(getApplicationContext(), soundNotification);
ringTone.play();
} catch (Exception e) {}
break;
default:
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alarmsReceived = (EditText) findViewById(R.id.alarmsReceivedEditText);
}
@Override
protected void onStart() {
super.onStart();
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent("com.AlarmManager.ALARM_ACTION");
pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
alarmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
alarmCounter++;
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT);
Notification ntfctn = new Notification(R.drawable.icon,
"Notification Title", System.currentTimeMillis());
ntfctn.setLatestEventInfo(context, "Notification Title",
"Notification Text", contentIntent);
notificationManager.notify(0, ntfctn);
Message msg = myHandler.obtainMessage(10, intent);
myHandler.sendMessage(msg);
}
};
IntentFilter filter = new IntentFilter("com.AlarmManager.ALARM_ACTION");
registerReceiver(alarmReceiver, filter);
}
@Override
protected void onResume() {
super.onResume();
// Place code here
}
public void startAlarm(View v) {
Calendar clndr = Calendar.getInstance();
clndr.setTimeInMillis(System.currentTimeMillis());
clndr.add(Calendar.MILLISECOND, 1000);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, clndr.
getTimeInMillis(), 5000, pendingIntent);
}
public void stopAlarm(View v) {
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "Alarm canceled.", Toast.LENGTH_SHORT).show();
}
}
//The main.xml:
//=============
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:textSize="16dp"
android:text="Alarm Counter:">
</TextView>
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:layout_marginRight="10dp"
android:textSize="16dp"
android:text="0"
android:enabled="false"
android:id="@+id/alarmsReceivedEditText"
android:textColor="#FFFFFF">
</EditText>
</TableRow>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<Button android:id="@+id/startAlarmButton"
android:layout_height="55dp"
android:layout_width="fill_parent"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="20dp"
android:text="Start Alarm"
android:onClick="startAlarm" />
<Button
android:id="@+id/stopAlarmButton"
android:layout_height="55dp"
android:layout_width="fill_parent"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="20dp"
android:text="Stop Alarm"
android:onClick="stopAlarm" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
假设您有一个工作startAlarm
,您可以尝试:
@Override
protected void onResume() {
super.onResume();
startAlarm(null);
}
答案 1 :(得分:0)
暂停您的活动时使用:
@Override
protected void onPause() {
super.onPause();
Intent alarmIntent = new Intent(MainTabActivity.this, MyReceiver.class);
pendingIntent_ = PendingIntent.getBroadcast(MainTabActivity.this, 0, alarmIntent, 0);
cancel();
}
和简历时:
@Override
protected void onResume() {
super.onResume();
start();
}
和函数start()和cancel()的代码:
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 4000, interval, pendingIntent_);
// Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent_);
// Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}