Notification 最简单的使用

package com.example.mynotification;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button btnNotificationShow, btnNofity;
	private NotificationManager notificationManager;
	private NotificationCompat.Builder builder;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		initView();
		initEvent();
	}


	private void initEvent() {
		btnNotificationShow.setOnClickListener(new OnClickListener() {

			

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//
				// Toast.makeText(MainActivity.this, "00000", 0).show();

				builder = new NotificationCompat.Builder(MainActivity.this);
				Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/")); //这里负责的是跳转,可以跳转到Native的activity
				//当然也可以跳转到一个url
				PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, mIntent, 0);
				builder.setContentIntent(pendingIntent);
				builder.setSmallIcon(R.drawable.ic_launcher);
				builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
				builder.setAutoCancel(true);
				builder.setContentTitle("普通通知");
				builder.setContentText("天要下雨的最新通知,请知悉!!");
				// MainActivity.this.selectNotofovatiomLevel(builder);
				// MainActivity.this.selectNotofovatiomLevel(builder);
				notificationManager.notify(0, builder.build());

			}
		});

		btnNofity.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				//能否修改通知的消息
				builder.setContentText("我这里修改了通知的消息!!晴天了");
			
				notificationManager.notify(0, builder.build());
			}
		});
	}

	private void initView() {

		btnNotificationShow = (Button) findViewById(R.id.button1);
		btnNofity = (Button) findViewById(R.id.btn_notify);
	}

}

你可能感兴趣的:(Android)