主布局文件:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通通知"
android:onClick="click1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大视图通知"
android:onClick="click2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带进度条通知"
android:onClick="click3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义通知"
android:onClick="click4" />
</LinearLayout>
</span>
前两种通知跳转的第二个布局页面
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Second" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
</span>
自定义通知用到的布局文件:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv"
android:layout_below="@id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv2"
android:layout_toRightOf="@id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</span>
主逻辑代码:
<span style="font-size:18px;">package com.example.day11_notifition;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View v)
{
//创建消息对象
NotificationCompat.Builder notifition=new NotificationCompat.Builder(getApplicationContext());
//必有属性
notifition.setSmallIcon(R.drawable.ic_launcher);
notifition.setContentTitle("新消息");
notifition.setContentText("李易峰:你吃饭了吗?");
//可有属性
Bitmap bp=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
notifition.setLargeIcon(bp);
notifition.setContentInfo("info");
notifition.setWhen(System.currentTimeMillis());
Intent intent=new Intent(MainActivity.this,Second.class);
//延迟意图
PendingIntent pit=PendingIntent.getActivity(getApplicationContext(), 1, intent,PendingIntent.FLAG_ONE_SHOT);
notifition.setContentIntent(pit);
NotificationManager nma=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nma.notify(1,notifition.build());
}
public void click2(View v)
{
NotificationCompat.Builder noti=new NotificationCompat.Builder(MainActivity.this);
noti.setContentTitle("标题");
noti.setContentText("内容");
noti.setSmallIcon(R.drawable.ic_launcher);
NotificationCompat.InboxStyle sty=new NotificationCompat.InboxStyle();
sty.addLine("111");
sty.addLine("222");
sty.addLine("333");
noti.setStyle(sty);
Intent intent=new Intent(MainActivity.this,Second.class);
//延迟意图
PendingIntent pt=PendingIntent.getActivity(getApplicationContext(), 1, intent,PendingIntent.FLAG_ONE_SHOT);
//将意图放到通知
noti.setContentIntent(pt);
//通过系统服务获取通知的管理者对象
NotificationManager mg=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//通过管理者发送消息
mg.notify(1, noti.build());
}
public void click3(View v)
{//没有跳第二个页面
final NotificationCompat.Builder noti=new NotificationCompat.Builder(getApplicationContext());
noti.setSmallIcon(R.drawable.ic_launcher);
noti.setContentTitle("进度条通知");
noti.setContentText("正在下载");
final NotificationManager mg=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
new Thread()
{
@Override
public void run()
{
for (int i = 0; i <100; i+=10) {
noti.setProgress(100,i,false);
mg.notify(1,noti.build());
}
noti.setContentText("下载完成");
mg.notify(1,noti.build());
}
}.start();
}
public void click4(View v)
{//没有跳第二个页面
NotificationCompat.Builder noti=new NotificationCompat.Builder(MainActivity.this);
noti.setSmallIcon(R.drawable.ic_launcher);
noti.setContentTitle("自定义通知");
//将自定义xml转换成RemoteViews
RemoteViews rv=new RemoteViews(getPackageName(), R.layout.zidiyi_notifition);
rv.setImageViewResource(R.id.iv,R.drawable.ic_launcher);
rv.setTextViewText(R.id.tv1,"标题");
rv.setTextViewText(R.id.tv2,"内容");
noti.setContent(rv);
//有将信息发送到通知栏的作用
NotificationManager mg=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mg.notify(1,noti.build());
}
}
</span>
前两种通知跳转的第二个逻辑代码页面
<span style="font-size:18px;">package com.example.day11_notifition;
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.view.Menu;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
NotificationManager nfm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//nfm.cancel(1);//取消固定id的通知
nfm.cancelAll();
}
}
</span>