码迷,mamicode.com
首页 > 移动开发 > 详细

android应用多线程守护让你很难杀死它

时间:2014-11-11 16:45:10      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:android

1、android 应用开启后启动一个服务

public class TestserviceActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
System.out.println("activity运行的线程id:"+ Thread.currentThread().getName() +"--"+
Thread.currentThread().getId());
System.out.println("activity的进程id:"+android.os.Process.myPid());
        
        Intent intent = new Intent(this,Service1.class);
        startService(intent);
    }

}

2、第一个服务

public class Service1 extends Service {


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("服务1 被开启");
System.out.println("服务运行的线程id:"+ Thread.currentThread().getName() +"--"+
Thread.currentThread().getId());
System.out.println("服务的进程id:"+android.os.Process.myPid());
super.onCreate();
}

@Override
public void onDestroy() {
Intent intent = new Intent(this,Service2.class);
startService(intent);
super.onDestroy();
}


}

3、第2个服务

public class Service2 extends Service {


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
System.out.println("服务2 被开启");
super.onCreate();
}

@Override
public void onDestroy() {
Intent intent = new Intent(this,Service1.class);
startService(intent);
super.onDestroy();
}
}

4、清单文件中

 <service android:name=".Service1"
            android:process="cn.it.yqq"
            ></service>
        <service android:name=".Service2"></service>

android应用多线程守护让你很难杀死它

标签:android

原文地址:http://blog.csdn.net/u014600432/article/details/41011345

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!