android双后台服务,消息通知类(service1)
2016-01-09 17:42
591 查看
package com.service.demo;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentCallbacks;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
/**
*
* @author hellogv
*
*/
public class Service1 extends Service
{
private String TAG = getClass().getName();
// 用于判断进程是否运行
private String Process_Name = "com.example.servicetest2:service2";
/**
*启动Service2
*/
private StrongService startS2 = new StrongService.Stub()
{
@Override
public void stopService() throws RemoteException
{
Intent i = new Intent(getBaseContext(), Service2.class);
getBaseContext().stopService(i);
}
@Override
public void startService() throws RemoteException
{
Intent i = new Intent(getBaseContext(), Service2.class);
getBaseContext().startService(i);
}
};
@Override
public void onTrimMemory(int level)
{
Toast.makeText(getBaseContext(), "Service1 onTrimMemory..."+level, Toast.LENGTH_SHORT).show();
keepService2();//保持Service2一直运行
}
@Override
public void onCreate()
{
Toast.makeText(Service1.this, "Service1 onCreate...", Toast.LENGTH_SHORT).show();
keepService2();
new Thread()
{
int i=0;
public void run()
{
while(true)
{
ServiceUtils.showBigImgViewNotification(Service1.this,"测试标题","测试内容","123");
try
{
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
/**
* @获取默认的pendingIntent,为了防止2.3及以下版本报错
* @flags属性:
* 在顶部常驻:Notification.FLAG_ONGOING_EVENT
* 点击去除: Notification.FLAG_AUTO_CANCEL
*/
public PendingIntent getDefalutIntent(int flags){
PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);
return pendingIntent;
}
/**
* 判断Service2是否还在运行,如果不是则启动Service2
*/
private void keepService2()
{
boolean isRun = ServiceUtils.isProessRunning(Service1.this, Process_Name);
if (isRun == false)
{
try
{
Toast.makeText(getBaseContext(), "重新启动 Service2", Toast.LENGTH_SHORT).show();
startS2.startService();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent)
{
return (IBinder) startS2;
}
}
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentCallbacks;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
/**
*
* @author hellogv
*
*/
public class Service1 extends Service
{
private String TAG = getClass().getName();
// 用于判断进程是否运行
private String Process_Name = "com.example.servicetest2:service2";
/**
*启动Service2
*/
private StrongService startS2 = new StrongService.Stub()
{
@Override
public void stopService() throws RemoteException
{
Intent i = new Intent(getBaseContext(), Service2.class);
getBaseContext().stopService(i);
}
@Override
public void startService() throws RemoteException
{
Intent i = new Intent(getBaseContext(), Service2.class);
getBaseContext().startService(i);
}
};
@Override
public void onTrimMemory(int level)
{
Toast.makeText(getBaseContext(), "Service1 onTrimMemory..."+level, Toast.LENGTH_SHORT).show();
keepService2();//保持Service2一直运行
}
@Override
public void onCreate()
{
Toast.makeText(Service1.this, "Service1 onCreate...", Toast.LENGTH_SHORT).show();
keepService2();
new Thread()
{
int i=0;
public void run()
{
while(true)
{
ServiceUtils.showBigImgViewNotification(Service1.this,"测试标题","测试内容","123");
try
{
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
/**
* @获取默认的pendingIntent,为了防止2.3及以下版本报错
* @flags属性:
* 在顶部常驻:Notification.FLAG_ONGOING_EVENT
* 点击去除: Notification.FLAG_AUTO_CANCEL
*/
public PendingIntent getDefalutIntent(int flags){
PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);
return pendingIntent;
}
/**
* 判断Service2是否还在运行,如果不是则启动Service2
*/
private void keepService2()
{
boolean isRun = ServiceUtils.isProessRunning(Service1.this, Process_Name);
if (isRun == false)
{
try
{
Toast.makeText(getBaseContext(), "重新启动 Service2", Toast.LENGTH_SHORT).show();
startS2.startService();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent)
{
return (IBinder) startS2;
}
}
相关文章推荐
- android 双后台服务,消息通知 (mainActivity)
- android双后台服务,消息通知类
- android 下雪
- ANDROID内存优化(大汇总——下)
- Android环境下安装openCV
- Android Tip : Android GestureDetector 手势识别 抬起 操作
- ANDROID内存优化(大汇总——中)
- Android开发技术周报
- Android - Menu
- Android开发技术周报
- ANDROID内存优化(大汇总——上)
- android选择本地图片并裁剪
- android值radioGroup实现多行多列单选框--动态加入
- 酷派大神9976A刷机
- android之旅26 JNI概念与运用
- AndroidManifest.xml
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
- Android Studio debug使用release的签名
- Android 中的 Intent 简介