0
我已經定製notification
remoteviews
,該視圖包含佈局和兩個按鈕,我想要的是當用戶點擊一個名爲「 contentLayout「時,將打開活動」Test「,當用戶單擊按鈕」alertsTenMinButton「或」remindLaterButton「時,動作」Test2「將開始。現在問題是活動」Test「正確啓動,但對於活動「Test2」,當我點擊通知欄中的按鈕時,似乎什麼都沒有發生,但是當我點擊「後退」時,當通知欄折回時,我可以看到Test2已經啓動,所以問題是如何當用戶點擊遠程視圖中的按鈕時,使通知欄折回?下面 是我的代碼:Android遙控器帶按鈕,點擊按鈕時,通知欄不能打包
MainActivity:
package com.example.androidtest2;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
MainActivity.this).setSmallIcon(R.drawable.ic_launcher);
String notifyTitle = "title....";
String notifyContent = "content......";
RemoteViews remoteViews = new RemoteViews(MainActivity.this
.getPackageName(), R.layout.notification_bar);
remoteViews.setTextViewText(R.id.title, notifyTitle);
remoteViews.setTextViewText(R.id.content, notifyContent);
Intent resultIntent = new Intent(MainActivity.this, Test.class);
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(MainActivity.this);
stackBuilder.addParentStack(Test.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
Intent notifyLaterIntent = new Intent(MainActivity.this,
NotificationProcesssingReceiver.class);
PendingIntent PendingNotifyLaterIntent = PendingIntent
.getBroadcast(MainActivity.this, 0, notifyLaterIntent,
PendingIntent.FLAG_CANCEL_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
Intent notify10LaterIntent = new Intent(MainActivity.this,
NotificationProcesssingReceiver.class);
PendingIntent PendingNotify10LaterIntent = PendingIntent
.getBroadcast(MainActivity.this, 0,
notify10LaterIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.contentLayout,
resultPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.remindLaterButton,
PendingNotifyLaterIntent);
remoteViews.setOnClickPendingIntent(R.id.remindTenMinButton,
PendingNotify10LaterIntent);
NotificationManager mNotificationManager = (NotificationManager) MainActivity.this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
notification.defaults = Notification.DEFAULT_SOUND;
notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "ticker text...";
notification.bigContentView = remoteViews;
// mId allows you to update the notification later on.
mNotificationManager.notify(1, notification);
}
});
}
}
NotificationProcesssingReceiver:
package com.example.androidtest2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class NotificationProcesssingReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
Intent test = new Intent(context, Test2.class);
test.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//context.startActivity(toMainScreen);
context.startActivity(test);
}
}
測試:
public class Test extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
/* NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1);*/
}
}
的Test2:
public class Test2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
}
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="56dp"
android:text="Button" />
</RelativeLayout>
notification_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/desk_command_oye" />
<LinearLayout
android:id="@+id/contentTextLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView1"
android:orientation="vertical" >
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title...."/>
<TextView android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text content"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/buttonsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_toRightOf="@+id/linearLayout1" >
<Button android:id="@+id/remindTenMinButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"/>
<Button android:id="@+id/remindLaterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
activity_test.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
activity_test2.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
的AndroidManifest.xml: