2011-04-26 114 views
0

這裏是我的代碼:無法停止服務。

package com.callplus; 

import java.io.IOException; 

import android.app.Activity; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.database.sqlite.SQLiteDatabase; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.IBinder; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 

public class AudioRecordTest extends Service { 
    private static String TAG = "AudioRecordTest"; 
    private static String FileName = null; 
    private MediaRecorder Recorder = null; 
    public static String callerName, date, path,number; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     Log.d(TAG, "I am in Oncreate"); 
     DBAdapter db = new DBAdapter(this); 
     db.open(SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
     db.getPath(); 
     path = db.audioPath; 
     db.close(); 

     final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     telephony.listen(new PhoneStateListener() { 
      public void onCallStateChanged(final int state, 
        final String incomingNumber) { 
       try { 
        switch (state) { 
        case TelephonyManager.CALL_STATE_IDLE: 
         if (Recorder != null) { 
          Recorder.release(); 
          Recorder.stop(); 
         } 
         Log.d("TestActivity", "Call is idle."); 
         actionStop(getApplicationContext()); 
         break; 
        case TelephonyManager.CALL_STATE_OFFHOOK: 
         Log.d("TestActivity", "Call connected"); 
         startRecording(); 
         break; 
        case TelephonyManager.CALL_STATE_RINGING: 
         break; 
        default: 
         break; 

        } 
       } finally { 

       } 
      } 
     }, PhoneStateListener.LISTEN_CALL_STATE); 

    } 

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

    @Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 
     stopSelf(); 

    } 
    public static void actionStop(Context ctx) 
    { 
    Intent i = new Intent(ctx, AudioRecordTest.class); 
    Log.d(TAG, "I am in ActionStop"); 
// i.setAction(ACTION_STOP); 
    ctx.startService(i); 
    } 
} 

我想打電話時怠速停止服務。

+0

它不是workng still.what應該怎麼辦?並在ACTION_STOP.It說ACTION_STOP不能解決。 – Hitesh 2011-04-26 09:11:07

回答

1

嗨in actionStop方法而不是stopService方法調用您已經再次調用startService方法。

ctx.stopService(i); 

使用上述線路的而不是和電話響應