第一次海報在這裏,請原諒我冗長的帖子 - 我想提供所有必要的信息。android通知總是重新啓動活動
我想通過點擊通知功能來實現夢寐以求的簡歷,但我遇到了一些困難。首先,詳細信息(下面的代碼):
項目屬性
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
- MyApp的經由startService 啓動 MyAppService
- MyAppService的onStartCommand函數使用NotificationCompat.Builder目的是在下拉通知中創建一個欄。
行爲我想實現:
- 啓動了通知欄條目(例如郵件)
- 開始MyApp的(這也將啓動MyAppService)的應用程序
- 拉下通知欄並選擇MyAppService(是的,MyApp當前正在屏幕上
- 通知欄應該只是滾動不做任何事情
我注意到的是,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獲得的代碼,看看它是否可行 - 在每個CAS中e,最上面的應用程序將黑屏最小化,MyApp將最大化並調用MainActivity.java的onCreate函數。
我只想通知下拉菜單向上滾動,如果MyApp已經是最上面的。
在此先感謝!
當它是最頂層的活動時,爲自己的應用程序提供通知並不是一個好主意;通知用於通知用戶與其他應用程序相關的某些內容。我建議您在活動排在最前面時取消應用的任何通知。 – dsandler 2013-04-11 13:24:31
哦,而且,如果你也使用'startForeground()',你不需要自己調用'notify()';後者的電話將負責發佈通知。 – dsandler 2013-04-11 13:25:02
hey @dsandler,通知實際上是由'MyAppService.java'維護的,這是'MyApp'啓動時啓動的一項服務。 – Mark 2013-04-11 19:11:02