我的Android应用程序目前有问题。我使用Google客户端消息服务向我的Android应用程序发送消息。如果用户打开应用,则会从HomeActivity
开始。
当用户现在从服务器收到我的消息并按下推送消息时,应用程序会使用AlertActivity
启动新的意图。
此时,我将putExtra
的时间(保存在消息中)传递给新活动,我将其用于CownDownTimer
。直到这一刻,一切正常,倒计时工作完美。
现在的问题是,当用户最小化应用程序并再次打开它时。
在此点,用户不会再次重定向到我的AlertActivity
,而是重定向到我的HomeActivity
。
在CountDownTimer
结束后播放AlertSound
,但他无法取消,因为他无法再次显示该活动。
如果用户在AlertActivity
正在运行时启动应用,我如何将用户重定向到CountDown
?
我的AlertActivity
package com.prgguru.example;
public class AlertActivity extends Activity {
String str;
Boolean running;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert);
final TextView txttime = (TextView) findViewById(R.id.txtalert);
str = getIntent().getStringExtra("msg");
Integer time = Integer.parseInt(str);
time = time; //*60000;
txttime.setText(time+"");
CountDownTimer Count = new CountDownTimer(time, 1000) {
@Override
public void onTick(long millisUntilFinished) {
running = true;
txttime.setText(String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
//txttime.setText(""+millisUntilFinished / 1000);
}
@Override
public void onFinish() {
txttime.setText("Finished");
final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
}
};
Count.start();
}
public boolean getRunning(){
return false;
}
}
GCMIntentService
public class GCMIntentService extends IntentService {
// Sets an ID for the notification, so it can be updated
public static final int notifyID = 9001;
NotificationCompat.Builder builder;
public GCMIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
sendNotification(extras.get(ApplicationConstants.MSG_KEY)+"");
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
Intent resultIntent = new Intent(this, AlertActivity.class);
resultIntent.putExtra("msg", msg);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mNotifyBuilder;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Alert")
.setContentText("You've received new message.")
.setSmallIcon(R.drawable.ic_launcher);
// Set pending intent
mNotifyBuilder.setContentIntent(resultPendingIntent);
// Set Vibrate, Sound and Light
int defaults = 0;
//defaults = defaults | Notification.DEFAULT_LIGHTS;
//defaults = defaults | Notification.DEFAULT_VIBRATE;
// defaults = defaults | Notification.DEFAULT_SOUND;
Notification notification = mNotifyBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alarm);
notification.defaults |= Notification.DEFAULT_VIBRATE;
mNotifyBuilder.setDefaults(defaults);
// Set the content for Notification
mNotifyBuilder.setContentText("New message from Server");
// Set autocancel
mNotifyBuilder.setAutoCancel(true);
// Post a notification
mNotificationManager.notify(notifyID, notification);
}
}