更改通知布局

33

我反编译了我的系统音乐应用程序(来自索尼爱立信的Android GB 2.3.7),因为我想要更改通知布局。我找到了使用以下代码创建通知的方法:

private void sendStatusBarNotification(Track paramTrack)
{
    if (paramTrack != null)
    {
        NotificationManager localNotificationManager = (NotificationManager)this.mContext.getSystemService("notification");
        String str = paramTrack.getArtist();

        if ((str == null) || (str.equals(this.mContext.getString(2131361954))))
            str = this.mContext.getString(2131361798);

        Notification localNotification = new Notification(2130837696, paramTrack.getTitle() + " - " + str, System.currentTimeMillis());
        localNotification.flags = (0x2 | localNotification.flags);
        localNotification.flags = (0x20 | localNotification.flags);

        PendingIntent localPendingIntent = PendingIntent.getActivity(this.mContext, 0, new Intent(this.mContext, MusicActivity.class), 268435456);
        localNotification.setLatestEventInfo(this.mContext, paramTrack.getTitle(), str, localPendingIntent);
        localNotificationManager.notify(0, localNotification);
    }
}

我现在的问题是:如何更改通知布局?我想要构建一个外观类似于原始Android通知布局但右侧有额外图像的布局。我该如何实现?

4个回答

85

首先为您的通知创建一个XML文件。

custom_notification.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        style="Custom Notification Title" />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        style="Custom Notification Text" />
</RelativeLayout>

现在是Java代码:

public class MainActivity extends Activity {

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, "Custom Notification", when);

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        mNotificationManager.notify(1, notification);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
希望这个东西对你有用。
编辑:如果你遇到像这样的问题,你也可以访问。
此外,您可以访问此处了解更多信息。
编辑于2016年4月26日:NotificationCompat.Builder可用于创建以下Notification实例:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(icon)
            .setContent(contentView)
            .setContentTitle("Custom Notification")
            .setWhen(when);
...
mNotificationManager.notify(1, notificationBuilder.build());

谢谢你。你会得到奖励,但我需要一个小时后才能给你 ;) - Cilenco
2
@Cilenco 没问题亲爱的。我并没有为了赏金而帮助你。我只是感到高兴,我的解决方案对你有用,就这样... :) 我喜欢用我所知道的知识来帮助人们。愉快编码...! - Chintan Soni
@shree202 给了我一个建议。我使用 RemoteView 构建了一个通知大视图来控制播放/暂停,就像这个链接(stackoverflow.com/questions/14508369/…)中的示例一样。所有的都没问题,但是当我点击设备的返回按钮并从应用程序退出后,点击事件(播放/暂停/前进/关闭)按钮就不起作用了。请帮助我。 - Helal Khan
如何在此方法中每10秒更新TextView? - Meysam
1
@IgorGanapolsky 感谢您指出旧代码。先生,我已更新答案。如果我错了,请随时纠正我。 - Chintan Soni
显示剩余2条评论

5

这里附上了两张截图,第一张屏幕内容是帖子的标题。当我们点击应用程序名称右侧的向下箭头时,它会跳转到第二个屏幕截图。第二个截图展示的是推送通知的自定义布局。以下是我为我的设备设计的示例布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:background="#80000000"
    android:layout_height="wrap_content">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:layout_marginLeft="5dp"
        android:background="@null"
        android:layout_gravity="center_vertical|center_horizontal"
        android:scaleType="centerCrop"/>
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:minHeight="48dp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:background="@android:color/transparent"
        android:textColor="@android:color/white"
        tools:text="Test"/>
</LinearLayout>
<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:contentDescription="@null"
    android:scaleType="centerCrop"
    android:src="@drawable/placeholder"/>
</LinearLayout>

使用自定义布局创建通知的方法
public static void createNotification(String title, String body,String image_url, Context context, int notificationsId, String single_id) {
    Intent notificationIntent;

    long when = System.currentTimeMillis();
    int id = (int) System.currentTimeMillis();

    Bitmap bitmap = getBitmapFromURL(image_url);
    NotificationCompat.BigPictureStyle notifystyle = new NotificationCompat.BigPictureStyle();
    notifystyle.bigPicture(bitmap);
    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
    contentView.setImageViewBitmap(R.id.image, bitmap);
    contentView.setTextViewText(R.id.title, body);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setStyle(notifystyle)
            .setCustomBigContentView(contentView)
            .setContentText(body);
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    notificationIntent = new Intent(context, SinglePost.class);
    notificationIntent.putExtra("single_id",single_id);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(context, id, notificationIntent, 0); 

    Notification notification = mBuilder.build();
    notification.contentIntent = contentIntent;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;


    mNotificationManager.notify(notificationsId, notification);

}

public static Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

太棒了,夥計,幫了很多忙。 - Sachin Varma

3
要创建自定义通知布局,请查看Android API指南有关此主题的帮助页面。看起来你要使用RemoteViews类。

0

如果我们能够获取布局数据,无论是在代码还是xml中指定的,那就太好了。

根据提供的内容,我可以说你必须获取图像并将其放入new Notification(2130837696,paramTrack.getTitle() + " - " + str,System.currentTimeMillis())声明中。

老实说,根据你所提供的信息,这就是我能给你的全部。祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接