Android使用通知(Notification)功能

版权声明:有问题可联系博主QQ:15577969,大家一起相互交流和学习。 https://blog.csdn.net/qq15577969/article/details/80862570

MainActivity.java

package com.t20.notification;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	/**
	 * 通知(兼容Android2.3.3版本)
	 * @param v
	 */
	public void notify(View v){
		//获取系统服务:通知服务(NotificationManager是通知管理器)
		NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		//创建通知对象 
		Notification notification=new Notification(R.drawable.ic_launcher, "你收到了一条短消息", System.currentTimeMillis());
		//-------------------------------------------------图标------------通知状态栏的提示文字-------弹通知的时间-----------
		//System.currentTimeMillis()表示当前系统时间
		
		//创建意图

		//隐式启动(发送短信)
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_SENDTO);
		intent.setData(Uri.parse("smsto:18172310617"));
		intent.putExtra("sms_body", "这是要发送的短信内容");
		
		PendingIntent contentIntent=PendingIntent.getActivity(MainActivity.this, 100, intent, PendingIntent.FLAG_ONE_SHOT);
		//--------------------------------------------------------上下文--------请求码--标准意图---------通知显示的 模式----
		
		//设置通知内容
		notification.setLatestEventInfo(MainActivity.this, "小美的短消息", "在吗?", contentIntent);
		//----------------------------------上下文--------------标题--------内容----点击后要到达的意图
		nm.notify(1, notification);
	}

}

猜你喜欢

转载自blog.csdn.net/qq15577969/article/details/80862570