2014-08-28 54 views
0

我已经定制notificationremoteviews,该视图包含布局和两个按钮,我想要的是当用户点击一个名为“ 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

​​

回答

0

实测值的问题,使用以下代码:

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
context.sendBroadcast(it); 

具体交易,检查this link