通知栏通知:
startService(new Intent(this, NotKillService.class));
public class NotKillService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Notification notification = new Notification();
//通知栏没有展开时的显示内容
notification.icon = R.mipmap.dyyd_icon;//通知图片
notification.tickerText = "这里是通知内容";
notification.flags = Notification.FLAG_SHOW_LIGHTS;
//下拉通知栏的显示内容
notification.contentView = new RemoteViews(getPackageName() , R.layout.notifition_view);
//点击通知栏跳转到相应的应用里面
Intent intent = new Intent(this , MainActivity.class);
//这一句加不加没什么影响
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notification.contentIntent = PendingIntent.getActivity(this , 1 , intent , 0);
//这里的id不能是0
startForeground(1 , notification);
//acquireWakeLock(getApplicationContext());
}
@Override
public void onDestroy() {
super.onDestroy();
//releaseWakeLock();
}
PowerManager.WakeLock mWakeLock;
//申请设备电源锁
private void acquireWakeLock(Context context)
{
if (null == mWakeLock)
{
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "WakeLock");
if (null != mWakeLock)
{
mWakeLock.acquire();
}
}
}
//释放设备电源锁
private void releaseWakeLock()
{
if (null != mWakeLock)
{
mWakeLock.release();
mWakeLock = null;
}
}
}