【问题标题】:Android remoteViews with buttons , when click the button, the notification bar can not pack upAndroid 带按钮的remoteViews,点击按钮时,通知栏无法收起
【发布时间】:2014-08-28 05:41:05
【问题描述】:

我用remoteviews自定义了notification,视图包含布局和两个按钮,我想要的是当用户单击名为“contentLayout”的布局时,将打开“测试”活动,并且当用户单击按钮“remindTenMinButton”或“remindLaterButton”时,将启动“Test2”操作。现在问题是“Test”活动正确启动,但对于“Test2”活动,当我单击通知栏中的按钮时,似乎什么也没发生,但是当我点击“返回”时,当通知栏向后折叠时,我可以看到Test2已经开始,所以问题是当用户点击按钮时如何让通知栏向后折叠远程视图? 以下是我的代码:

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

}

}

测试2

    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

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidtest2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.androidtest2.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.androidtest2.Test"
        android:label="@string/title_activity_test" >
    </activity>

    <receiver android:name="com.example.androidtest2.NotificationProcesssingReceiver" >
    </receiver>

    <activity
        android:name="com.example.androidtest2.Test2"
        android:label="@string/title_activity_test2" >
    </activity>
</application>

【问题讨论】:

    标签: java android android-layout button click


    【解决方案1】:

    找到问题,使用下面的代码:

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

    详情请查看this link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多