通過remoteViews自定義Notification通知欄的佈局
阿新 • • 發佈:2019-02-20
remoteViews,是一種遠端view,通過跨程序更新自己的介面,主要用於通知欄和桌面小部件的開發過程中。
1、通知欄的實現
public class MainActivity extends Activity implements View.OnClickListener {
private Button btn_one,btn_two,btn_three,btn_four;
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
btn_one = (Button) findViewById(R.id.btn_one);
btn_one.setOnClickListener(this);
btn_two = (Button) findViewById(R.id.btn_two);
btn_two.setOnClickListener(this);
btn_three = (Button) findViewById(R.id .btn_three);
btn_three.setOnClickListener(this);
btn_four = (Button) findViewById(R.id.btn_four);
btn_four.setOnClickListener(this);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onClick(View view) {
switch (view.getId ()) {
case R.id.btn_one://預設通知
PendingIntent pendingIntent1=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification1=new Notification();
notification1.icon=R.drawable.head;
notification1.when=System.currentTimeMillis();
notification1.tickerText="您有新短訊息,請注意查收!";
notification1.contentIntent=pendingIntent1;
manager.notify(1,notification1);
break;
case R.id.btn_two://API11之後使用
PendingIntent pendingIntent2=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification2=new Notification.Builder(this)
.setSmallIcon(R.drawable.head)//即便自定義了圖示,但有時候還是顯示系統預設的,應該與手機型別有關
.setContentTitle("好訊息")
.setContentText("API11之後的通知")
.setTicker("hello world")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent2)
.setNumber(1) //在最右側現實。這個number起到一個序列號的作用,如果多個觸發多個通知(同一ID),可以指定顯示哪一個。
.getNotification(); //build()是在API16及之後增加的,在API11中可以使用getNotificatin()來代替
manager.notify(1,notification2);
break;
case R.id.btn_three://API16之後使用
PendingIntent pendingIntent3=PendingIntent.getActivity(this,0,new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification3=new Notification.Builder(this)
.setSmallIcon(R.drawable.head)
.setContentTitle("好訊息")
.setContentText("API16之後的通知")
.setTicker("hello world")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent3)
.setNumber(1)
.build();
manager.notify(1,notification3);
break;
case R.id.btn_four://自定義通知,
Notification notification = new Notification();
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.tickerText = "hello world";
notification.icon = R.drawable.head;//這是個坑,如果不設定icon,通知不顯示
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remote_layout);
remoteViews.setTextViewText(R.id.tv_title, "請假條");
remoteViews.setTextViewText(R.id.tv_content, "世界這麼大,我想去看看");
remoteViews.setImageViewResource(R.id.iv_head, R.drawable.head);
notification.contentIntent = pendingIntent;
notification.contentView = remoteViews;
manager.notify(1, notification);
break;
}
}
}
通知欄的佈局remote_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_head"
android:layout_width="40dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標題" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:text="內容。。。。" />
</LinearLayout>
</LinearLayout>