【问题标题】:Send Intent from Broadcast Receiver with Extras to Activity (Problems with a Service)从带有附加功能的广播接收器向 Activity 发送 Intent(服务问题)
【发布时间】:2011-11-16 20:26:31
【问题描述】:

我一直在寻找这么多,但我没有找到解决方案。

我的应用程序一直拨打一个号码,当它收到一个准确号码的来电时,它会停止(死亡)。 我的想法是:

  1. Activity 启动执行调用工作的服务
  2. 等待来电的 BroadcastReceiver

所以,我想使用 BroadcastReceiver 来杀死 Activity,但我还没有找到任何解决方案。为了尝试另一件事,我尝试发送带有 Extras 的 Intent,但 Extras 已变为空。

我愿意使用任何其他解决方案!

非常感谢!

更新了执行 Kurtis Nusbaum 建议的代码 我在调用时发现问题出在 Service 中,所以我把所有代码都放了

这是我的代码:

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);
    }

}

广播接收器: 包 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: 包 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);
    }
}

非常感谢!

【问题讨论】:

  • 我已经看到问题出在服务中,所以我添加了代码期待任何帮助!

标签: android service android-activity android-intent broadcastreceiver


【解决方案1】:

如果您想从广播接收器完成活动,请在您的活动中创建另一个广播接收器来侦听特定意图。然后在您的第一个广播器接收器中,当您想要终止活动时,广播您的活动中的接收器正在侦听的 Intent。然后该接收器将被激活并可以完成所有活动。

这是一些伪代码:

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);
  }
}

【讨论】:

  • 非常感谢。为此,我做了一些与link 非常相似的事情,但它也不起作用,也没有收到 sendBroadcast。也许是来自 Manifest 的东西?您是否有任何在活动中成功使用 sendBroadcast 和接收的完整示例?再次感谢您!
  • 不客气,如果您觉得有帮助,请采纳答案。
  • 谢谢您,但还是没有解决,我已经根据您的建议更新了第一条消息中的代码,但仍然无法正常工作!谢谢!
  • 问题可能出在额外的意图上。他们的名字必须以包名开头...developer.android.com/reference/android/content/…, double[])
【解决方案2】:

问题出在:

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

它停止了接收器,所以我将其更改为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2018-09-06
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多