相关文档http://blog.youkuaiyun.com/vipzjyno1/article/details/25248021
##notification
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="发送消息"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private Button mButton1;
private NotificationManager mNotificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1= (Button) findViewById(R.id.button1);
mButton1.setOnClickListener(this);
mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)//API版本
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pend=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification=new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setTicker("我是一个消息")
.setContentTitle("我是一个标题").setContentText("我是内容").setContentInfo("你好啊").
setContentIntent(pend).setWhen(System.currentTimeMillis()).build();
mNotificationManager.notify(1,notification);
break;
default:
break;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButon1.findViewById(R.id.button1);
mButon1.setOnClickListener(this);
mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//初始化 NotificationManager
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
Notification notification=new Notification();//初始化Notofication
notification.icon=R.mipmap.ic_launcher;//设置初始化图片
notification.tickerText="我是一个消息";//设置初始化文本
notification.flags=Notification.FLAG_AUTO_CANCEL;
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pend=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(getApplicationContext(),"我是标题","我是内容",pend);
notification.when=System.currentTimeMillis();
mNotificationManager.notify(1,notification);
break;
default:
break;
}
}
####自定义notification
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是标题"/>
<ImageView
android:id="@+id/imageview_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/textview_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是内容"/>
</LinearLayout>
case R.id.button2:
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.activity_romoteviews);//自定义notification
Intent intent1=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pend1=PendingIntent.getActivity(getApplicationContext(), 1, intent1, PendingIntent.FLAG_ONE_SHOT);
Notification notification1=new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setTicker("我是一个消息")
.setContentTitle("我是一个标题").setContentText("我是内容").setContentInfo("你好啊").
setContentIntent(pend1).setWhen(System.currentTimeMillis()).setContent(remoteViews).build();
//自定义.setContent() mNotificationManager.notify(1,notification1);
----------