- package jason.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.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class MainActivity extends Activity {
-
Button button;int id = 0; -
@Override -
protected void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.activity_main); -
button = (Button) findViewById(R.id.notify); -
button.setOnClickListener(new OnClickListener() { -
-
@Override -
public void onClick(View arg0) { -
//获得通知管理器 -
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
-
//构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间) -
Notification notification = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
-
Intent intent = new Intent(MainActivity.this,MainActivity.class); -
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0); notification.setLatestEventInfo(getApplicationContext(), "通知标题", "通知显示的内容", pendingIntent); -
notification.flags = Notification.FLAG_AUTO_CANCEL;//点击后自动消失 -
notification.defaults = Notification.DEFAULT_SOUND;//声音默认 -
manager.notify(id, notification);//发动通知,id由自己指定,每一个Notification对应的唯一标志 -
//其实这里的id没有必要设置,只是为了下面要用到它才进行了设置 -
-
} -
}); -
} - }
安卓消息通知栏
最新推荐文章于 2024-07-15 10:34:46 发布