0

第一次在这里发帖,请原谅我的冗长帖子 - 我想提供所有必要的信息。

我正在尝试通过单击通知功能来实现梦寐以求的简历,但我遇到了一些困难。首先,详细信息(下面的代码):

  1. 项目属性

    Android Developer Tools Build: v21.1.0-569685
    Project Build Target : 4.2.2
    Platofrm : 4.2.2
    API Level : 17
    SDK Target : 17
    SDK min : 14
    
    Android Device Chooser : Android Device : Nexus 4 
    
  2. MyApp通过 startService启动MyAppService
  3. MyAppService的 onStartCommand 函数使用 NotificationCompat.Builder 对象在下拉通知中创建一个栏。

我试图实现的行为:

  1. 启动带有通知栏条目的应用程序(例如邮件)
  2. 启动 MyApp(这也将启动 MyAppService)
  3. 下拉通知栏并选择 MyAppService(是的,MyApp 当前在屏幕上
  4. 通知栏应该卷起来而不做任何事情

我注意到的是 MyApp 将最小化,而 MyApp 的新实例将最大化(类似于启动应用程序时的过渡)。当我在顶部有邮件应用程序(邮件应用程序最小化和 MyApp 最大化)时,我希望在 MyAppService 上选择这种行为。

有很多代码片段和建议,但我似乎无法让它工作;所以这是我的文件的内容:

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/eyeview"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.MyApp.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".service.MyAppService"
        android:label="MyAppService">
    </service>
</application>

MyApp/MainActivity.java

public static Intent serviceIntent;
public static Messenger clientMessenger;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstance);

    if (getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
    {
        finish();
        return;
    }

    setContentView(R.layout.activity_main);

    ...
    [stuff specific to app, like setting up Fragments]
    ...

    MainActivity.serviceIntent = new Intent(this, MyAppService.class);
    MainActivity.serviceIntent.putExtra("clientMessenger", clientMessenger);
    startService(MainActivity.serviceIntent);
}

MyAppService.java

@Override
private int onStartCommand(Intent intent, int flags, int startId)
{
    NotificationCompat.Builder ncB = new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.drawable.icon_app)
        .setContentTitle("MyAppService")
        .setSmallIcon(R.drawable.app);

    ncB.setContentText("Click to start MyApp");

    Intent nIntent = new Intent(getApplicationContext(), MainActivity.class);
    nIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    nIntent.setAction(Intent.ACTION_MAIN);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
    stackBuilder.addNextIntent(nIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    notificationBuilder.setContentIntent(resultPendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    int nId = 1;
    Notification notification = ncB.build();
    notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;

    notificationManager.notify(nId, notification); 

    sendMessengerToClient(intent);

    startForeground(nId, ncB.build());

    return Service.START_NOT_STICKY;
}

我希望我包含了所有足够的信息-我一直在调整从 SO 获得的代码,只是为了看看这是否可行-在每种情况下,最上面的应用程序都会最小化为黑屏,而 MyApp 会最大化,而 MainActivity.java 的 onCreate函数被调用。

如果 MyApp 已经是最上面的,我只想让通知下拉菜单向上滚动。

提前致谢!

4

2 回答 2

4
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);

通过设置PendingIntent.FLAG_UPDATE_CURRENTIntent.FLAG_ACTIVITY_SINGLE_TOP标志,我猜你可以实现这一点。如果它已经在运行,它将不会创建您的活动。

于 2013-05-10T13:03:09.983 回答
2

就我而言,Shashika 的答案有效,但以一种奇怪的方式。我不得不重新启动电话!:)

于 2015-05-14T09:35:53.313 回答