2016-09-22 2 views
1

私はこの問題の解決策を探しましたが、見つけられませんでした。Android - メインアクティビティを再表示する方法

私が抱えている問題は、アラームが鳴ったことです。ただし、MainActivityは閉じており、再表示されません。私は、次のコードでMainActivityを再表示するにはどうすればよい

public class AlarmReceiver extends WakefulBroadcastReceiver { 

@Override 
public void onReceive(final Context context, Intent intent) { 
    //this will update the UI with message 
    AlarmActivity inst = AlarmActivity.instance(); 
    inst.setAlarmText("Alarm! Wake up! Wake up!"); 

    //this will sound the alarm tone 
    //this will sound the alarm once, if you wish to 
    //raise alarm in loop continuously then use MediaPlayer and setLooping(true) 
    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
    if (alarmUri == null) { 
     alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    } 
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri); 
    ringtone.play(); 

    //this will send a notification message 
    ComponentName comp = new ComponentName(context.getPackageName(), 
      AlarmService.class.getName()); 
    startWakefulService(context, (intent.setComponent(comp))); 
    setResultCode(Activity.RESULT_OK); 
} 

}

コードは、私がJavaPapersからで動作する一例として、ダウンロードされたコードがある

http://javapapers.com/android/android-alarm-clock-tutorial/

+0

「AlarmService」のポストコードを教えてください。 – Pr38y

+0

コードはこちらです:http://javapapers.com/android/android-alarm-clock-tutorial/ – briano

答えて

0

私の提案 ブロードキャストを送信し、受信者/サービスにあなたのための活動

0

私は放送を試みましたが、それはうまくいかなかったので、私がやったことは不完全でした。

次のコードが機能しましたが(最後の3行が追加されました)、MainActivityの新しいインスタンスが作成されると推定されるため、間違っているようです。 MainActivityは元のバージョンではAlarmActivityです。

次のようにAlarmService.javaで「作品」であるとコード:

private void sendNotification(String msg) { 
    Log.d("AlarmService", "Preparing to send notification...: " + msg); 
    alarmNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, MainActivity.class), 0); 

    NotificationCompat.Builder alarmNotificationBuilder = new NotificationCompat.Builder(
      this).setContentTitle("Alarm").setSmallIcon(R.mipmap.ic_launcher) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
      .setContentText(msg); 

    alarmNotificationBuilder.setContentIntent(mPendingIntent); 
    alarmNotificationManager.notify(1, alarmNotificationBuilder.build()); 
    Log.d("AlarmService", "Notification sent."); 

    Intent mIntentMain = new Intent(this, MainActivity.class); 
    mIntentMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION); 
    this.startActivity(mIntentMain); 
} 

私は最後の3行で追加関連するコード。私はIntentServiceを拡張するので、これをAlarmService.javaに入れます。最初の投稿(上記)に投稿された元のコードは、WakefulBroadcastReceiverを拡張するAlarmReceiver.javaからのものです。私がしなければならないことは、既存のMainActivityを別のものを作成するのではなく表示させることです。

編集:29 - 9月 - 2016

主な活動は、すでに次のように私は最終的にこれを処理するためにやったことだった、表示される場合がありますので:

if (fn_IsMainActivityDisplayed(this) == false) { 
     System.out.println("Main activity is not running. Now recreating"); 
     try { 
      PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, 
        new Intent(this, MainActivity.class), PendingIntent.FLAG_ONE_SHOT); 
      mPendingIntent.send(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY); 
     } catch (Exception jExc) { 
  System.out.println("AlarmService: PendingIntent failed: Error = " + jExc.getMessage()); 
     } 
    } 

メインアクティビティが表示されているかどうかを判断するコードがSOに見つかりました。これが100%正しいかどうかは分かりませんが、正常に動作するようです。

関連する問題