0

我有一个具有以下活动执行逻辑的 Android 应用程序:

if FIRST_RUN: SplashScreeen -> 登录 -> MainScreenTabHost

if PUSH

  1. Not Running: SplashScreeen -> 登录 -> MainScreenTabHost
  2. Running:MainActivity收到消息

我不知道如何实现这一目标。我的项目源代码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运行启动画面并循环进行首次运行?另一方面,如果应用程序正在运行,我只想Intent为某些操作发送一个(可能是一个广播)。

还有一件事,我的服务并不总是在运行,因为它是由推送通知中的广播意图运行的。

4

2 回答 2

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 回答
0

From what I understand, you want to be able to run spashScreen or not, depending on the state of the application.
You could try to register a BroadcastReceiver on your main activity, register an IntentFilter for a custom filter (let's say: NOTIFICATION_INTENT)

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

Then, when creating notification you will have to create an intent to an notification activity that will broadcast the NOTIFICATION_INTENT.

The NotificationActivity should look like this:

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();
    }

}

The code for creating the notification will have something similar to this:

        // 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);

Hope this helps.

于 2012-10-31T12:00:18.563 回答