更改通知布局布局、通知

2023-09-11 20:09:29 作者:君浅吟

我反编译我的系统音乐应用程序(索尼爱立信为Android 2.3.7 GB),因为我想改变的通知布局。我发现,与此code创建通知的方式:

I decompiled my system music app (from Sony Ericsson for Android GB 2.3.7) because I want to change the notification layout. I found the method which creates the notification with this code:

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通知布局,但对通知的右侧额外的图像。我怎样才能做到这一点?

My question now is: How can I change the notification layout? I want to build a layout which looks like the original android notification layout but with an extra image on the right of the notification. How can I do this?

推荐答案

首先为您的通知创建XML。

First create an xml for your notification.

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 code:

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;
    }
}

希望这件事对你的作品。

Hope this thing works for you.

编辑:您还可以参观,如果你遇到问题,像this.

You can also visit if you come across problem like this.

此外,您还可以访问这里了解更多信息。

Also, you can visit here for more information.

 
精彩推荐
图片推荐