0

i am trying to create 2 applications.The 1st sends the imei via an implicit intent utilizing sendBroadcast. Below is the code for the first application.

package com.example.activity_1;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    TextView tv_imei;

    Button bt_getimei;
    String imei;

    public static ArrayList<String> numbers;
    private SubscriptionManager subscriptionManager;
    static final Integer PHONESTATS = 0x1;
    private final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_imei = (TextView) findViewById(R.id.tv_imei);

        bt_getimei = (Button) findViewById(R.id.bt_getimei);


        numbers = new ArrayList<String>();
        subscriptionManager = SubscriptionManager.from(MainActivity.this);

        bt_getimei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                askForPermission(Manifest.permission.READ_PHONE_STATE, PHONESTATS);

                tv_imei.setText(imei);

                Intent sendImei = new Intent();
                sendImei.addFlags(sendImei.FLAG_INCLUDE_STOPPED_PACKAGES);
                sendImei.setAction("com.example.activity_1");
                sendImei.putExtra(Intent.EXTRA_TEXT, imei);
                sendImei.setType("text/plain");
                sendBroadcast(sendImei);

            }
        });

    }
private void askForPermission(String permission, Integer requestCode) {
    if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {

        // Should show an explanation
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {

            ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);

        } else {

            ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
        }
    } else {
        imei = getImeiNumber();


        Toast.makeText(this, permission + " is already granted.", Toast.LENGTH_SHORT).show();
    }
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {

            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                imei = getImeiNumber();


            } else {

                Toast.makeText(MainActivity.this, "You have Denied the Permission", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}


private String getImeiNumber() {
    final TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //getDeviceId() is Deprecated so for android O we can use getImei() method
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return telephonyManager.getImei();
        }
        return telephonyManager.getImei();
    } else {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return telephonyManager.getImei();
        }
        return telephonyManager.getImei();
    }

}

}

I have created another application in which is included an broadcast receiver to receive the imei. Below is the code for application2

    package com.example.activity_2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import static android.content.Intent.EXTRA_TEXT;


public class MainActivity extends AppCompatActivity {


    TextView tv_imei;


    public class Receiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            StringBuilder imei= new StringBuilder();
            if (intent.getAction()==("com.example.activity_1")) {
                System.out.println("*****GOT THE INTENT********");
                StringBuilder append = imei.append("IMEI: " + intent.getExtras() + "\n");
                String log = imei.toString();
                Log.d("Received", log);
            }


        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_imei = (TextView) findViewById(R.id.tv_imei);


        // Get intent, action and MIME type
        IntentFilter IMEI = new IntentFilter();
        IMEI.addAction("com.example.activity_1");
        IMEI.setPriority(100);
        BroadcastReceiver br = new Receiver();
        registerReceiver(br,IMEI);



    }
}

I can't figure out why the 2nd application doesn't receive anything, because i am not at all familiar with java and programming in general.

Thank you in advance

1 Answers1

1

Briefly speaking, (For more details, refer this link)

Implicit intent are targeted by actions. So all broadcast receivers declared with that action will get invoked. Whereas,

Explicit intents are those which are directed to specific component. So only that specific component will receive the intent. In your case, it is Receiver and you will need to do few changes in your code to make it work.

Declare following in your manifest file.

 <receiver android:name=".MainActivity$Receiver">
    <intent-filter>
        <action android:name="com.example.activity_1"></action>
    </intent-filter>
</receiver>

and while sending the broadcast in your click listener. You also need to define that specific component (Receiver) to make the intent explicit. Otherwise the intent will be implicit and given that you are working on API 28, It won't work.

// pass Receiver class parameter to your Intent object to make the intent explicit.
Intent sendImei = new Intent(view.getContext(), Receiver.class); // change is required here
sendImei.addFlags(sendImei.FLAG_INCLUDE_STOPPED_PACKAGES);
sendImei.setAction("com.example.activity_1");
sendImei.putExtra(Intent.EXTRA_TEXT, imei);
sendImei.setType("text/plain");
sendBroadcast(sendImei);               
Binary Baba
  • 1,953
  • 2
  • 16
  • 23
  • Ok that makes sense.Unfortunately my assignment requests to do all this procedure utilizing implicit intents. Is this possible to achieve the above scenario with implicit intents. Thank you for your help and for your time commenting on this thread. – Μάριος Καμπόλης Mar 03 '19 at 12:59
  • What is your assignment exactly? If you have to use implicit intent then you need to use 'JobScheduler' or 'WorkManager' because of API 28 background execution limitation. – Binary Baba Mar 03 '19 at 13:07
  • I am requested to create one application that gets the imei's phone and afterwards using implicit intent to send it to another application.meanwhile i have to create a third application, that is going to intercept this communication and retrieves the imei essentially without someone figuring it out. – Μάριος Καμπόλης Mar 03 '19 at 15:15
  • 1
    Actually in API 28, the system strongly discourages sharing data in the background with other apps. One another way to do it is by dynamically registering the receiver [link](https://developer.android.com/guide/components/broadcasts#context-registered-receivers). You will be able to use implicit intent but this will require your app to be in memory. – Binary Baba Mar 05 '19 at 12:25
  • are you still looking for the solution? I found one work around. – Binary Baba Mar 09 '19 at 12:46
  • I found a solution and it was quite easy. All I had to do was to delete this line of code from the sender: sendImei.setType("text/plain"); The interceptor will have exactly the same broadcast receiver as activity_2. Thank you again for your time and for your useful comments. – Μάριος Καμπόλης Mar 10 '19 at 13:05
  • But how are you able to intercept the broadcast? Since from API level 28, you should not be able to receive implicit broadcast. Which version of Android are you using? – Binary Baba Mar 11 '19 at 04:18
  • I am using API 28. Look the assignment was for privacy protection survey. My scenario is that the 3rd app is running in the background. If the developer use the send broadcast command to send the imei then every other application with a listener is going to get the imei. – Μάριος Καμπόλης Mar 16 '19 at 19:31