Send data from BroadcastReceiver to unintentional main activity

0

How I am new, I have the following problem in android studio.
I want to identify an incoming call number that is displayed in the main activity, I am using BroadcastReceiver to detect the incoming number. Code where I use BroadcastReceiver that is in the call-in class:

   public void onReceive(Context context, Intent intent) {

    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        String numeroentrante = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        numero=numeroentrante;
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("MI_ESPECIFICA_ACTION");
        broadcastIntent.putExtra("numero", numero);
        context.sendBroadcast(broadcastIntent);

    }
    else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
        //Toast.makeText(context, "LLamada detenida",Toast.LENGTH_LONG).show();

    }


}

I show the Manifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="MI_ESPECIFICA_ACTION"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <receiver android:name=".util.LlamadEntrante">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

</application>

In the following code I show how it receives the data in the main activity.

BroadcastReceiver miReceptor;
IntentFilter intentFilter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
miReceptor=new MiBroadCastReceiver();
    intentFilter=new IntentFilter("MI_ESPECIFICA_ACTION");
}


@Override
    public void onResume(){
        super.onResume();
        registerReceiver(miReceptor,intentFilter);
}

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

    unregisterReceiver(miReceptor);
}

public class MiBroadCastReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent i){


        String dato1= i.getExtras().getString("numero");
        if (dato1 != null) {
            Toast.makeText(context,"numero entrante " + dato1,Toast.LENGTH_LONG).show();

            if (btSocket!=null)
            {
                try
                {
                    btSocket.getOutputStream().write("led2".toString().getBytes());
                }
                catch (IOException e)
                {
                    msg("Error");
                }
            }

        }


    }
}

Thanks everything is resolved, now I can use this data in the main activity ... corrected code.

    
asked by ave 28.07.2017 в 19:33
source

0 answers