您的位置:首页 > 其它

安卓通知的使用系列2:状态栏通知和自定义状态栏通知通知

2016-02-29 21:27 405 查看
状态栏通知是android开发中常见的一种通知形式,下面我们来介绍一下它的使用方法。

整体思路:在xml文件中放置两个button控件,分别设置它们的点击事件,声明NotificationManager对象和Notification.Builder对象,在onCreate方法中实例化这两个对象,在第一个点击事件中设置通知的各个属性并发送通知;在第二个点击事件中绑定一个自定义的xml'文件并发送通知。

activity_main.xml文件:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="135dp"
android:text="普通的通知使用" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:text="自定义通知的使用" />
custom_notification.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_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="100dp"
/>

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
/>

<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"
/>

</RelativeLayout>
MainActivity.java文件:

public class MainActivity extends Activity {

private Button button, button2;
private NotificationManager manager;
private Notification.Builder builder;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
// 创建一个通知管理类
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder = new Notification.Builder(this);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
MainActivity.class);
// 第二个参数表示请求的状态码,
PendingIntent contentIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent, 0);
builder.setContentIntent(contentIntent);
builder.setContentTitle("new notification is coming");
builder.setContentText("hello world");
builder.setTicker("有通知来了");// 第一次出现在状态栏的内容
builder.setSmallIcon(R.drawable.ic_launcher);
//			         通知的形式:闪光灯、声音、震动、自己设置的声音
//				也可以定义震动的形式,在api中都有介绍
// Notification.DEFAULT_ALL表示所有的提示都是默认的
// DEFAULT_LIGHTS默认的闪光,DEFAULT_SOUND默认的声音,DEFAULT_VIBRATE默认的震动
builder.setDefaults(Notification.DEFAULT_ALL);
// Uri uri=Uri.parse("file:///sdcard/notification/ringer.mp3");
// builder.setSound(uri);//提示用户可以自定义设置铃声
Notification notification = builder.build();// 仅仅限于在高版本4.1以上使用
// notification.defaults=Notification.DEFAULT_SOUND; //在低版本中使用
// 第一个参数表示通知的唯一标示符
manager.notify(1000, notification);
}
});

button2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
RemoteViews contentViews = new RemoteViews(getPackageName(),
R.layout.custom_notification);
contentViews.setImageViewResource(R.id.image,
R.drawable.ic_launcher);
contentViews.setTextViewText(R.id.title, "自定义通知的标题");
contentViews.setTextViewText(R.id.text, "自定义通知的内容");

Intent intent = new Intent(MainActivity.this,
MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setContent(contentViews);//指定自定义布局
Notification notification=builder.build();
manager.notify(10001,notification);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

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