这是简单实现运行项目,常驻通知一直显示在通知栏里。下面贴出代码
MainActivity:![这里写图片描述](http://img.blog.csdn.net/20160613155031569)
import com.example.adlayoutcode.R;
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.net.Uri;
import android.os.Bundle;
import android.widget.RemoteViews;
public class MainActivity extends Activity{
private NotificationManager notificationManager;
private int mNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNumber=(int)android.os.Build.VERSION.SDK_INT;//获取手机版本
setNotification();
finish();
}
private void setNotification() {
// 实例化一个notification
long when = System.currentTimeMillis();//获取当前系统时间
Notification notification = new Notification(R.drawable.ic_launcher, "",when);
// 不能用户不能手动清理掉
notification.flags = Notification.FLAG_NO_CLEAR;
// 设置用户点击notification的动作
// pendingIntent 延期的意图
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri url = Uri.parse("https://www.baidu.com/");
intent.setData(url);
//5.0以下默认使用系统浏览器打开连接
if(mNumber<20){
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, 0);
notification.contentIntent = pendingIntent;
// 自定义通知栏显示的界面
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.auto_test_activity);
notification.contentView = rv;
notificationManager.notify(0, notification);
}
}
`