android service 例子(電話錄音和獲取系統當前時間)
阿新 • • 發佈:2019-02-17
關於android service 的詳解請參考: android四大元件--android service詳解,下面將用兩個例項具體呈現Android Service的兩種實現。
一個是startService()方法來啟動一個服務,這裡用電話錄音的例子;
另一個是bindService()方法來繫結一個服務,這裡用獲取系統當前時間的例子;
例項一(電話錄音):
/CallRecorderService/res/layout/main.xml
/CallRecorderService/AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tvTemp" android:layout_width="fill_parent" android:layout_height="100dip" android:gravity="center" android:text="@string/hello" android:textSize="30dip" /> <Button android:id="@+id/startrecordservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="開啟電話錄音的功能" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zymic.callrecord" android:versionCode="1" android:versionName="1.0" > <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:name=".CallRecord01" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="CallRecordService" android:enabled="true" > </service> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" > </uses-permission> <uses-permission android:name="android.permission.RECORD_AUDIO" > </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> </manifest>
下面將是三個核心的實現
/CallRecorderService/src/com/bing/callrecord/CallRecordService.java
package com.bing.callrecord; import java.io.File; import java.io.IOException; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.MediaRecorder; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.widget.Toast; public class CallRecordService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); Toast.makeText(getApplicationContext(), "錄音服務已經建立!", Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(getApplicationContext(), "錄音服務已經銷燬!", Toast.LENGTH_LONG).show(); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Toast.makeText(getApplicationContext(), "錄音服務已經啟動!", Toast.LENGTH_LONG).show(); // TelephonyManager telephonymanager=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); telephonymanager.listen(new PhoneListener(getApplicationContext()), PhoneStateListener.LISTEN_CALL_STATE); } }
/CallRecorderService/src/com/bing/callrecord/PhoneListener.java
package com.bing.callrecord;
import java.io.File;
import java.io.IOException;
import android.content.Context;
import android.media.MediaRecorder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneListener extends PhoneStateListener {
File audioFile;
MediaRecorder mediaRecorder; //= new MediaRecorder();
Context c;
//
boolean iscall=false;
//
public PhoneListener(Context context){
c=context;
iscall=false;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
mediaRecorder = new MediaRecorder();
switch(state){
case TelephonyManager.CALL_STATE_OFFHOOK:
iscall=true;
try {
recordCallComment();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
mediaRecorder.stop();
}
Toast.makeText(c, "正在錄音", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_IDLE:
//if(mediaRecorder!=null){
//mediaRecorder.stop();
//mediaRecorder=null;
//}
if(iscall){
mediaRecorder.stop();
iscall=false;
}
break;
}
}
//
public void recordCallComment() throws IOException{
System.out.println(mediaRecorder);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
audioFile = File.createTempFile("record_", ".amr");
mediaRecorder.setOutputFile(audioFile.getAbsolutePath());
mediaRecorder.prepare();
mediaRecorder.start();
}
}
/CallRecorderService/src/com/bing/callrecord/CallRecorder.java
package com.bing.callrecord;
import com.zymic.callrecord.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CallRecorder extends Activity {
private Button beginrecordservice;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
beginrecordservice=(Button)findViewById(R.id.startrecordservice);
beginrecordservice.setOnClickListener(new BeginRecord());
}
//
private class BeginRecord implements OnClickListener{
@Override
public void onClick(View v) {
Intent serviceIntent=new Intent(getApplicationContext(),CallRecordService.class);
getApplicationContext().startService(serviceIntent);
}
}
}
最後效果圖如下:
例項二(獲取當前時間):
介面程式碼實現:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTemp"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:gravity="center"
android:textSize="30dip"
android:text="@string/hello" />
<Button
android:id="@+id/btnStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btnBind_name" />
<Button
android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btnUnbind_name" />
<TextView
android:id="@+id/tvInfo"
android:layout_width="fill_parent"
android:layout_height="100dip" />
</LinearLayout>
在Manifest裡面宣告服務,新增MyService宣告.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gel.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" android:exported="true"></service>
</application>
</manifest>
新增一個MyService類.程式碼如下:
package com.gel.service;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private MyService mMyService;
private TextView mTextView;
private Button bindServiceButton;
private Button unbindServiceButton;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
// 當我bindService時,讓TextView顯示MyService裡getSystemTime()方法的返回值
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mMyService = ((MyService.MyBinder) service).getService();
mTextView.setText("來自MyService的系統時間:"
+ mMyService.getSystemTime());
}
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
public void setupViews() {
mContext = MainActivity.this;
mTextView = (TextView)findViewById(R.id.tvInfo);
bindServiceButton = (Button) findViewById(R.id.btnStart);
unbindServiceButton = (Button) findViewById(R.id.btnStop);
bindServiceButton.setOnClickListener(this);
unbindServiceButton.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == bindServiceButton) {
Intent i = new Intent();
i.setClass(MainActivity.this, MyService.class);
mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
} else {
mContext.unbindService(mServiceConnection);
}
}
}
建立Bound Service,MyService程式碼:
package com.gel.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;
public class MyService extends Service {
// 定義個一個Tag標籤
private static final String TAG = "MyService";
// 這裡定義一個Binder類,用在onBind()有方法裡,這樣Activity那邊可以獲取到
private MyBinder mBinder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "Start IBinder!");
return mBinder;
}
@Override
public void onCreate() {
Log.e(TAG, "Start onCreate!");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.e(TAG, "Start onStart!");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
Log.e(TAG, "Start onDestroy!");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG, "Start onUnbind!");
return super.onUnbind(intent);
}
// 這裡我寫了一個獲取當前時間的函式,
public String getSystemTime() {
Time t = new Time();
t.setToNow();
return t.toString();
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
}
最後的效果如下: