Notification的使用
2016-01-25 11:09
393 查看
1.普通的通知
package mobile.android.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
public class Main extends Activity
{
private NotificationManager mNotificationManager;
public void onClick_ShowCommonNotification(View view)
{
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.smile).setContentTitle("笑脸通知")
.setContentText("图标显示在左侧!");
mNotificationManager.notify(1, builder.build());
}
public void onClick_ShowBigIconNotification(View view)
{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.smile);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.why).setLargeIcon(bitmap)
.setContentTitle("大图标笑脸").setContentText("大图标放在左侧,小图标放在右下角!");
mNotificationManager.notify(2, builder.build());
}
public void onClick_ShowBigPictureNotification(View view)
{
// 必须先清除大图Notification,否则再次显示时抛出异常
mNotificationManager.cancel(3);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.big);
Notification notification = new Notification.BigPictureStyle(
new Notification.Builder(this).setContentTitle("Big Picture")
.setContentText("大图").setSmallIcon(R.drawable.wrath))
.bigPicture(bitmap).build();
mNotificationManager.notify(3, notification);
}
public void onClick_ShowInfoNotification(View view)
{
Notification.Builder builder1 = new Notification.Builder(this)
.setSmallIcon(R.drawable.why).setContentTitle("Info")
.setContentText("文本信息显示在右下角!").setContentInfo("信息");
mNotificationManager.notify(4, builder1.build());
// setContentInfo优先,有setContentInfo,setNumber不起作用
Notification.Builder builder2 = new Notification.Builder(this)
.setSmallIcon(R.drawable.wrath).setContentTitle("Number")
.setContentText("数字显示在右下角!").setNumber(25);
mNotificationManager.notify(5, builder2.build());
}
public void onClick_ShowProgressNotification(View view)
{
Notification.Builder builder1 = new Notification.Builder(this)
.setSmallIcon(R.drawable.smile).setContentTitle("Determinate Progress")
.setContentText("显示进度的进度条!").setProgress(100, 25, false);
mNotificationManager.notify(6, builder1.build());
Notification.Builder builder2 = new Notification.Builder(this)
.setSmallIcon(R.drawable.why).setContentTitle("Indeterminate Progress")
.setContentText("未显示进度的进度条!").setProgress(100, 25, true);
mNotificationManager.notify(7, builder2.build());
}
public void onClick_ClearAllNotification(View view)
{
mNotificationManager.cancelAll();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
}2.自定义Notification
布局文件Notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="自定义内容"
android:textColor="#F00" android:textSize="20sp" android:gravity="center" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/smile"
android:layout_gravity="center" />
</LinearLayout>
Java代码
3.结合Pendingintent
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_notification);
Intent intent=new Intent(this,ExpandableListViewActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification=new Notification.Builder(this).setContentTitle("通知")
.setContentText("1021589")
.setSmallIcon(R.drawable.wait)
.setDefaults(Notification.DEFAULT_ALL)
.setContent(remoteViews)
.setContentIntent(pendingIntent)
.build();
manager.notify(1,notification);
package mobile.android.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
public class Main extends Activity
{
private NotificationManager mNotificationManager;
public void onClick_ShowCommonNotification(View view)
{
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.smile).setContentTitle("笑脸通知")
.setContentText("图标显示在左侧!");
mNotificationManager.notify(1, builder.build());
}
public void onClick_ShowBigIconNotification(View view)
{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.smile);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.why).setLargeIcon(bitmap)
.setContentTitle("大图标笑脸").setContentText("大图标放在左侧,小图标放在右下角!");
mNotificationManager.notify(2, builder.build());
}
public void onClick_ShowBigPictureNotification(View view)
{
// 必须先清除大图Notification,否则再次显示时抛出异常
mNotificationManager.cancel(3);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.big);
Notification notification = new Notification.BigPictureStyle(
new Notification.Builder(this).setContentTitle("Big Picture")
.setContentText("大图").setSmallIcon(R.drawable.wrath))
.bigPicture(bitmap).build();
mNotificationManager.notify(3, notification);
}
public void onClick_ShowInfoNotification(View view)
{
Notification.Builder builder1 = new Notification.Builder(this)
.setSmallIcon(R.drawable.why).setContentTitle("Info")
.setContentText("文本信息显示在右下角!").setContentInfo("信息");
mNotificationManager.notify(4, builder1.build());
// setContentInfo优先,有setContentInfo,setNumber不起作用
Notification.Builder builder2 = new Notification.Builder(this)
.setSmallIcon(R.drawable.wrath).setContentTitle("Number")
.setContentText("数字显示在右下角!").setNumber(25);
mNotificationManager.notify(5, builder2.build());
}
public void onClick_ShowProgressNotification(View view)
{
Notification.Builder builder1 = new Notification.Builder(this)
.setSmallIcon(R.drawable.smile).setContentTitle("Determinate Progress")
.setContentText("显示进度的进度条!").setProgress(100, 25, false);
mNotificationManager.notify(6, builder1.build());
Notification.Builder builder2 = new Notification.Builder(this)
.setSmallIcon(R.drawable.why).setContentTitle("Indeterminate Progress")
.setContentText("未显示进度的进度条!").setProgress(100, 25, true);
mNotificationManager.notify(7, builder2.build());
}
public void onClick_ClearAllNotification(View view)
{
mNotificationManager.cancelAll();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
}2.自定义Notification
布局文件Notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="自定义内容"
android:textColor="#F00" android:textSize="20sp" android:gravity="center" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/smile"
android:layout_gravity="center" />
</LinearLayout>
Java代码
package mobile.android.custom.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.RemoteViews; public class Main extends Activity { private NotificationManager mNotificationManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } public void onClick_ShowNotification(View view) { RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification); remoteViews.setTextViewText(R.id.textview, "更新自定义内容"); Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.drawable.smile).setContent(remoteViews); mNotificationManager.notify(1, builder.build()); } }
3.结合Pendingintent
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_notification);
Intent intent=new Intent(this,ExpandableListViewActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification=new Notification.Builder(this).setContentTitle("通知")
.setContentText("1021589")
.setSmallIcon(R.drawable.wait)
.setDefaults(Notification.DEFAULT_ALL)
.setContent(remoteViews)
.setContentIntent(pendingIntent)
.build();
manager.notify(1,notification);
相关文章推荐
- Codeforces Round #340 (Div. 2) by komendart
- 《JAVA与模式》之简单工厂模式
- 月光博客: 个人用户如何安全地选择软件
- learn python the hard way(笨办法学python) 练习40 类的用法
- c#异常处理机制
- Android中的SharedPreferences存储数据方式
- linux修改hostname
- java基础篇--04<java基础类库>
- 在python3.3后urllib2已经不能再用,只能用urllib.request来代替
- OOP (Objects and Classes )
- FreeRTOS系列第14篇---FreeRTOS任务通知
- java中文乱码解决之道(九)—–总结
- 【链表】Linked List Cycle II
- python 常用模块
- 企业自托管代码管理gitlab的安装和相关配置
- Android studio Git功能使用教程
- xml格式解析
- Java数组的创建和使用
- String-常见工具收集
- java中文乱码解决之道(七)-----JSP页面编码过程