基本notification使用例子
package com.example.notificationsimple;
import android.R.integer;
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.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button btnadd;
Button btncancel;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取NotificationManager
final NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
btnadd=(Button) findViewById(R.id.btnadd);
btnadd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获取noBuilder
NotificationCompat.Builder noBuilder=new NotificationCompat.Builder(MainActivity.this);
noBuilder.setTicker("这是什么玩意");
noBuilder.setSmallIcon(R.drawable.ic_launcher);
noBuilder.setContentTitle("德义发了一条消息");
noBuilder.setContentText("德义最帅了");
noBuilder.setContentInfo("假的");
//设置Notification点击后可以自动删除
noBuilder.setAutoCancel(true);
Intent intent=new Intent(MainActivity.this,MyMsgActivity.class);
//设置通知栏的点击跳转
noBuilder.setContentIntent(pIntent);
//获取notification
Notification notification=noBuilder.build();
//显示通知栏,第一个参数为通知栏id,在删除的时候调用id
manager.notify(i,notification);
i++;
}
});
btncancel=(Button) findViewById(R.id.btncancel);
btncancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
manager.cancelAll();
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}