package visizen.com.notification;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 旧版notification创建方法
*
* @param view
*/
public void click(View view) {
//System.out.println("old!");
//获取系统通知管理者
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
/**
* Constructs a Notification object with the information needed to
* have a status bar icon without the standard expanded view.
*
* @param icon The resource id of the icon to put in the status bar.
* @param tickerText The text that flows by in the status bar when the notification first
* activates.
* @param when The time to show in the time field. In the System.currentTimeMillis
* timebase.
*
* @deprecated Use {@link Builder} instead.
*/
Notification n = new Notification(R.drawable.notification, "我是滚动文本!", System.currentTimeMillis());
n.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//这里编译不过,不知道怎么处理
//n.setLatestEventInfo(this, "我是大的标题", "我是标题下面的内容", pendingIntent);
nm.notify(0, n);
}
/**
* 新版notification创建方法
*
* @param view
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void click2(View view) {
//System.out.println("new!");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
//1.获取系统通知的管理者
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//2.用notification工厂 创建一个notification
Notification noti = new Notification.Builder(this)
.setContentTitle("我是大的标题")
.setContentText("我是内容")
.setSmallIcon(R.drawable.notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setContentIntent(contentIntent)
.build();
//3.把notification显示出来
nm.notify(1, noti);
}
}