您的位置:首页 > 产品设计 > UI/UE

Android UI基础——Notification控件

2016-04-07 08:52 806 查看
Notification是手机状态栏的通知,显示在手机最上方,是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。

接下来通过具体的代码实现Notification来介绍属性等内容,代码中会通过三个按钮的监听事件来发送Notification,系统默认的的Notification使用增加的Notification.Builder类,使用该类创建Notification对象。自定义的Notification就使用Notification类创建对象。两种创建方法还是有点区别的,可以在代码中慢慢体会,代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mButton1,mButton2,mButton3;
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotificationManager服务
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mButton1 = (Button) findViewById(R.id.button_default);
mButton2 = (Button) findViewById(R.id.button_custom);
mButton3 = (Button) findViewById(R.id.button_clean);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_default:
Notification.Builder builder = new Notification.Builder(this);//创建Notification.builder对象
builder.setTicker("有一条新消息");//设置显示在状态栏的通知提示信息
builder.setSmallIcon(R.mipmap.ic_launcher);//设置通知的图标
builder.setContentTitle("Notification标题");//设置通知内容的标题
builder.setContentText("这是Notification的内容");//设置通知内容
builder.setAutoCancel(true);//设置打开通知,该通知自动消失
//PendingIntent是Intent的包装,是在外部执行的用于跳转页面,但不是马上跳转,在下面会有详细解释
PendingIntent pendingIntent = PendingIntent.getActivities(this,0, new Intent[]{new Intent(MainActivity.this, MainActivity.class)},0);
builder.setContentIntent(pendingIntent);
manager.notify(0,builder.build());
break;
case R.id.button_custom:
Notification notification = new Notification();//创建Notification对象
notification.icon = R.mipmap.dialog_1;
notification.tickerText = "自定义的一条新消息";
notification.flags = Notification.FLAG_AUTO_CANCEL;//设置打开通知,该通知自动消失
//RemoteViews类描述了一个View对象能够显示在其他进程中,可以融合从一个 layout资源文件实现布局
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notify);
remoteViews.setImageViewResource(R.id.image,R.mipmap.ic_launcher);
remoteViews.setTextViewText(R.id.text1,"自定义notify的内容");
remoteViews.setTextViewText(R.id.text2,"自定义notify第二行内容");
PendingIntent pendingIntent1 = PendingIntent.getActivities(this,1, new Intent[]{new Intent(MainActivity.this, MainActivity.class)},0);
notification.contentIntent = pendingIntent1;
notification.contentView = remoteViews;
manager.notify(2,notification);
break;
case R.id.button_clean:
manager.cancelAll();//删除通知
}
}
}


注:PendingIntent的详细介绍,引用连接如下:

http://blog.csdn.net/zeng622peng/article/details/6180190

自定义Notification的布局代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorAccent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@+id/image">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>


效果图如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android ui 控件 notificati