简体   繁体   English

从广播接收器向活动发送意图(活动存在问题)

[英]Send Intent from Broadcast Receiver with Extras to Activity (Problems with a Service)

I have been looking for that so much and I haven't found the solution. 我一直在寻找这么多东西,但没有找到解决方案。

My applications calls a number all time, and when it receives an incoming call with an exactly number it stops (dies). 我的应用程序始终会呼叫一个号码,当它收到具有确切号码的来电时,它将停止(死亡)。 My idea is that: 我的想法是:

  1. Activity launches Service that does the job of calling 活动启动执行呼叫工作的服务
  2. A BroadcastReceiver that is expecting for the incoming call 期待来电的BroadcastReceiver

So, I want to use the BroadcastReceiver to kill the Activity, but I haven't found any solution to this. 因此,我想使用BroadcastReceiver杀死Activity,但是我没有找到任何解决方案。 To try another thing, I tried to send an Intent with Extras but the Extras have become null. 为了尝试另一件事,我尝试发送一个具有Extras的Intent,但是Extras已变为空。

I'm open to use any other solution that solve it! 我愿意使用任何其他解决方案!

Thank-you very very much! 非常非常感谢你!

UPDATED CODE TO DO THE RECOMMENDATION OF Kurtis Nusbaum I see that the problem is in the Service when it makes the call, so I put all my code 更新代码以进行Kurtis Nusbaum的推荐 我看到问题出在调用时服务中,所以我把所有代码都放在了

Here my code: 这是我的代码:

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; BroadcastReceiver:软件包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;
    }
}

And Manifest: 和清单:

    <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>

The Service Sercivio.java: package com.comunicacio; 服务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; */
    }
}

And finally Call.java: package com.comunicacio; 最后是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);
    }
}

Thank-you very very much! 非常非常感谢你!

If you want to finish and activity from a broadcast receiver, create another broadcast receiver in your activity that listens for a particular intent. 如果您要完成并从广播接收器进行活动,请在您的活动中创建另一个广播接收器,以监听特定的意图。 Then in your first broadcaster receiver, when ever you want to kill the activity, broadcast the Intent that the receiver in your activity is listening for. 然后,在您想杀死该活动的第一个广播接收器中,广播活动中的接收器正在监听的Intent。 That receiver will then get activated and can all finish on the activity. 然后,该接收器将被激活,并可以完成所有活动。

Here's some pseudo code: 这是一些伪代码:

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

The problem was on: 问题出在:

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

It stopped the Receiver, so I have changed it to: 它停止了接收器,所以我将其更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM