@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v("BACKGROUND", "程序进入后台");
showNotification();
}
// 点击HOME键时程序进入后台运行
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
// 按下HOME键
if(keyCode == KeyEvent.KEYCODE_HOME){
// 显示Notification
notification = new NotificationExtend(this);
notification.showNotification();
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
package com.test.background;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
/**
* Notification扩展类
* @Description: Notification扩展类
* @File: NotificationExtend.java
* @Package com.test.background
*/
public class NotificationExtend {
private Activity context;
public NotificationExtend(Activity context) {
// TODO Auto-generated constructor stub
this.context = context;
}
// 显示Notification
public void showNotification() {
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (
NotificationManager)context.getSystemService(
android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification = new Notification(
R.drawable.icon,"阅读器",
System.currentTimeMillis());
// 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_ONGOING_EVENT;
// 表明在点击了通知栏中的"清除通知"后,此通知自动清除。
notification.flags |= Notification.FLAG_AUTO_CANCEL
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 5000;
// 设置通知的事件消息
CharSequence contentTitle = "阅读器显示信息"; // 通知栏标题
CharSequence contentText = "推送信息显示,请查看……"; // 通知栏内容
Intent notificationIntent = new Intent(context,context.getClass());
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(
context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(
context, contentTitle, contentText, contentIntent);
// 把Notification传递给NotificationManager
notificationManager.notify(0, notification);
}
// 取消通知
public void cancelNotification(){
NotificationManager notificationManager = (
NotificationManager) context.getSystemService(
android.content.Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}
}
<activity android:name="ShowMessageActivity"
android:launchMode="singleTask"></activity>
public class MyApplication extends Application{
public int count = 0;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityStopped(Activity activity) {
Log.v("viclee", activity + "onActivityStopped");
count--;
if (count == 0) {
Log.v("viclee", ">>>>>>>>>>>>>>>>>>>切到后台 lifecycle");
}
}
@Override
public void onActivityStarted(Activity activity) {
Log.v("viclee", activity + "onActivityStarted");
if (count == 0) {
Log.v("viclee", ">>>>>>>>>>>>>>>>>>>切到前台 lifecycle");
}
count++;
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.v("viclee", activity + "onActivitySaveInstanceState");
}
@Override
public void onActivityResumed(Activity activity) {
Log.v("viclee", activity + "onActivityResumed");
}
@Override
public void onActivityPaused(Activity activity) {
Log.v("viclee", activity + "onActivityPaused");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.v("viclee", activity + "onActivityDestroyed");
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.v("viclee", activity + "onActivityCreated");
}
});
}
}
//用来控制应用前后台切换的逻辑
private boolean isCurrentRunningForeground = true;
@Override
protected void onStart() {
super.onStart();
if (!isCurrentRunningForeground) {
Log.d(TAG, ">>>>>>>>>>>>>>>>>>>切到前台 activity process");
}
}
@Override
protected void onStop() {
super.onStop();
isCurrentRunningForeground = isRunningForeground();
if (!isCurrentRunningForeground) {
Log.d(TAG,">>>>>>>>>>>>>>>>>>>切到后台 activity process");
}
}
public boolean isRunningForeground() {
ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
// 枚举进程
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
if (appProcessInfo.processName.equals(this.getApplicationInfo().processName)) {
Log.d(TAG,"EntryActivity isRunningForeGround");
return true;
}
}
}
Log.d(TAG, "EntryActivity isRunningBackGround");
return false;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有