0

次のアクティビティ実行ロジックを備えたAndroidアプリケーションがあります。

if FIRST_RUN:SplashScreeen->ログオン-> MainScreenTabHost

if PUSH

  1. Not Running:SplashScreeen->ログオン-> MainScreenTabHost
  2. RunningMainActivityメッセージを受信します

これを達成する方法がわかりません。NotificationBarアイテムの私のソースコード:

Notification notif = new Notification(R.drawable.ic_launcher, "Text in status bar", 
System.currentTimeMillis());
Intent intent = new Intent(this, MainScreenTabHost.class);//HERE PROBLEM
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
notif.setLatestEventInfo(this, "Notification's title", "Notification's text", pIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
this.notificationManager.notify(1, notif);

このコードは常にアプリケーションを実行しますMainScreenTabHostが、アプリケーションが実行されていない場合は、ログオン画面で初期化されます。

私の質問は、アプリケーションが実行されていることを確認し、実行されていない場合は、Intentスプラッシュ画面を実行してFIRSTRUNを繰り返す方法を教えてください。一方、アプリケーションが実行されている場合はIntent、いくつかの操作のために(おそらくブロードキャスト)を送信したいだけです。

そして、1つは、プッシュ通知からのブロードキャストインテントによって実行されるため、私のサービスが常に実行されているとは限りません。

4

2 に答える 2

0

私の理解では、アプリケーションの状態に応じて、spashScreenを実行できるようにするかどうかを指定します。
メインアクティビティにBroadcastReceiverを登録し、カスタムフィルターにIntentFilterを登録することができます(たとえば、NOTIFICATION_INTENT)

IntentFilter intentFilter = new IntentFilter(NotificationActivity.NOTIFICATION_INTENT);         // notification click     intent
this.registerReceiver(mReceiver, intentFilter);

次に、通知を作成するときに、NOTIFICATION_INTENTをブロードキャストする通知アクティビティへのインテントを作成する必要があります。

NotificationActivityは次のようになります。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class NotificationActivity extends Activity{

    public static final String NOTIFICATION_INTENT = "notification_clicked_intent"; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.notification);

         // broadcast intent
         Intent i = new Intent();
         i.setAction(NOTIFICATION_INTENT);
         getBaseContext().sendBroadcast(i);

         /* Set activity result and send intent data */
         setResult(RESULT_OK, getIntent());

         /* Finish activity and return to parent activity */
         finish();
    }

}

通知を作成するためのコードは、次のようになります。

        // set intent to be opened on NotificationClick
        notificationIntent = new Intent(this, NotificationActivity.class);
        notificationIntent.putExtra("key", icon);

        PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ACTIVITY_RESULT, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

お役に立てれば。

于 2012-10-31T12:00:18.563 に答える
0

うーん、私は次の解決策を見つけました:

// Check current activity
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(this.getPackageName())) {
            Intent broadCast = new Intent(MainScreenTabHost.BROADCAST_ACTION);
            broadCast.putExtras(arg1.getExtras());
            sendBroadcast(broadCast);
        } else {

            this.sendNotif(arg1);
        }

つまり、トリックは次のとおりです。onMessageメソッドを起動すると、現在Topにあるアクティビティを確認し、それが私の一部であるかどうかを確認します。ブロードキャストを送信するだけです。SplashScreenインテントを使用した通知を表示しない場合。

于 2012-10-31T12:05:42.183 に答える