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>