Androidサービス入門(電話傍受)
4935 ワード
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:onClick="Stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
<Button
android:onClick="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
</LinearLayout>
package com.example.listencall;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, PhoneStatusService.class);
startService(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void Stop(View v)
{
//
// creat startCommand start destory
//
Intent intent = new Intent(this, PhoneStatusService.class);
stopService(intent);
}
public void Start(View v)
{
//
// creat startCommand start destory
// Destory startCommand start
//
Intent intent = new Intent(this, PhoneStatusService.class);
startService(intent);
}
}
package com.example.listencall;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneStatusService extends Service
{
//
@Override
public void onCreate()
{
super.onCreate();
Log.i("Service", " ……");
//
// /
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//
tm.listen(new MyPhoneStatusLister(), PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStatusLister extends PhoneStateListener
{
private MediaRecorder recorder;
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
try
{
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE://
if (null != recorder)
{
recorder.stop();
recorder.reset(); // You can reuse the object by going
recorder.release(); // Now the object cannot be reused
System.out.println(" ……");
}
break;
case TelephonyManager.CALL_STATE_RINGING://
//
recorder = new MediaRecorder();
System.out.println(" :" + incomingNumber + " ……");
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setOutputFile("/sdcard/" + incomingNumber + ".aac");
recorder.prepare();
break;
case TelephonyManager.CALL_STATE_OFFHOOK://
if (null != recorder)
{
System.out.println(" ……");
recorder.start(); // Recording is now started
}
break;
default:
break;
}
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
super.onCallStateChanged(state, incomingNumber);
}
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.i("Service", " ……");
}
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
@Deprecated
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
Log.i("Service", " Start……");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// TODO Auto-generated method stub
Log.i("Service", " onStartCommand……");
return super.onStartCommand(intent, flags, startId);
}
}