2

我一直在尋找這麼多,我還沒有找到解決方案。從廣播接收器發送意圖與附加活動(問題與服務)

我的應用程序一直都會撥打一個號碼,當它收到一個正好有數字的來電時,它會停止(終止)。 我的想法是:

  1. 活動啓動服務,只要調用
  2. 所期待的來電

所以廣播接收器的工作,我想使用的BroadcastReceiver殺活動,但我還沒有找到任何解決方案。爲了嘗試另一件事,我試着發送一個附加意圖,但附加功能已經變爲空。

我打算使用任何解決方案的其他解決方案!

非常感謝你!

更新的代碼TO DO裴家努斯鮑姆的建議 我看到的問題是,當它使呼叫的服務,所以我把我所有的代碼

這裏我的代碼:

package com.comunicacio; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.util.Log; 

public class Comunicacio extends Activity { 

    IntentFilter filter; 

    private BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.i("BYE", "OOOOOOOOOOOOK"); 
      finish(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     filter = new IntentFilter(); 
     filter.addAction("com.comunicacio.FINALITZAR"); 

     startService(new Intent(this, Servicio.class)); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     registerReceiver(receiver, filter); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     unregisterReceiver(receiver); 
    } 

} 

The BroadcastReceiver: package com.comunicacio;

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.util.Log; 

public class DetectarCridada extends BroadcastReceiver { 
    public static final String CUSTOM_INTENT = "com.comunicacio.FINALITZAR"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle bundle = intent.getExtras(); 

     if (bundle == null) 
      return; 

     String state = bundle.getString(TelephonyManager.EXTRA_STATE); 

     if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) { 
      String phonenumber = bundle 
        .getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 

      Log.i("BYE", "UUUUUUUUUUUUUUUUL"); 

      // if (es_acceptat(phonenumber)) { 
      Intent i = new Intent(); 
      i.setAction(CUSTOM_INTENT); 
      context.sendBroadcast(i); 
      // } 
     } 
    } 

    private boolean es_acceptat(String telefono) { 
     if (telefono.equals("123")) 
      return true; 
     return false; 
    } 
} 

和清單:

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

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

     <receiver android:name=".DetectarCridada" > 
      <intent-filter > 
       <action android:name="android.intent.action.PHONE_STATE" /> 
      </intent-filter> 
     </receiver> 
     <service 
      android:enabled="true" 
      android:name=".Servicio" /> 

     <activity android:name=".Call" /> 
    </application> 

    <uses-permission android:name="android.permission.CALL_PHONE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
</manifest> 

的服務Sercivio.java: 包com.comunicacio;

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.os.IBinder; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 

public class Servicio extends Service { 
    private static final String TAG = "MyService"; 
    boolean hem_cridat = false; 
    int telefono = 123; 

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

    @Override 
    public void onCreate() { 
     Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onCreate"); 
     EndCallListener callListener = new EndCallListener(); 
     TelephonyManager mTM = (TelephonyManager) this 
       .getSystemService(Context.TELEPHONY_SERVICE); 
     mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE); 
    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onStart(Intent intent, int startid) { 
     Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
     call(); 
    } 

    private class EndCallListener extends PhoneStateListener { 
     private static final String LOG_TAG = "Comunicacio:"; 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      switch (state) { 
      case TelephonyManager.CALL_STATE_RINGING: 
       Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
       break; 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
       Log.i(LOG_TAG, "OFFHOOK"); 
       hem_cridat = true; 
       break; 
      case TelephonyManager.CALL_STATE_IDLE: 
       Log.i(LOG_TAG, "IDLE"); 
       if (hem_cridat) { 
        hem_cridat = false; 
        try { 
         Log.i(LOG_TAG, "Cridant!"); 
         call(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
       break; 
      } 
     } 
    } 

    private void call() { 
    /* THE PROBLEM IS THERE !!! If I don't do that, it works! */ 
    /* Intent i = new Intent(getBaseContext(), Call.class); 
     i.putExtra("NUMERO", telefono); 
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     getApplication().startActivity(i); 
     ++telefono; */ 
    } 
} 

最後Call.java: package com.comunicacio;

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 

public class Call extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bundle b = getIntent().getExtras(); 
     Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" 
       + b.getInt("NUMERO"))); 
     startActivity(i); 
    } 
} 

非常感謝你!

+0

我已經看到,問題出在Service中,所以我添加了代碼,希望有任何幫助! – kanashin

回答

1

問題是關於:

@Override 
protected void onPause() { 
    super.onPause(); 
    unregisterReceiver(receiver); 
} 

它停止了接收器,所以我把它改爲:

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(receiver); 
} 
4

如果你想完成一個廣播接收器的活動並在你的活動中創建另一個廣播接收器來監聽特定的意圖。然後,在您的第一臺廣播接收機中,如果您想要終止該活動,請播放您的活動中的接收機正在監聽的意圖。該接收器將被激活,並可以完成該活動。

下面是一些僞代碼:

public class YourActivity extends Activity{ 

    private class CloseLisntener extends BroadcastReceiver{ 
    onReceive(Contetxt context, Intent intent){ 
     YourActivity.this.finish(); 
    } 
    } 

    private CloseListener closeListener; 

    protected void onCreate(Intent icicle){ 
    closeListener = new CloseListener(); 
    // other on create stuff 
    } 

    protected void onResume(){ 
    registerReceiver(closeListener, /* Your intent filter goes here */); 
    } 

    protected void onPause(){ 
    unregisterReceiver(closeListener); 
    } 
} 
+0

非常感謝。要做到這一點,我做了一些非常類似於[鏈接](http://stackoverflow.com/questions/4555215/android-broadcastreceiver-within-activity)的東西,它也行不通,不會收到sendBroadcast。也許是來自清單的東西?你有任何使用sendBroadcast和在活動中接收成功的完整例子嗎?再次感謝你! – kanashin

+0

不客氣,請接受答案,如果你發現它有幫助。 –

+0

感謝您,但仍未解決,我已經使用您的建議更新了第一封郵件中的代碼,但仍然無法使用!謝謝! – kanashin