Я пытаюсь создать 2 приложения. Первый отправляет IMEI через неявное намерение, используя sendBroadcast.Ниже приведен код для первого приложения.
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();
}
}
}
Я создал другое приложение, в которое включен широковещательный приемник для получения imei.Ниже приведен код для 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);
}
}
Я не могу понять, почему второе приложение ничего не получает, потому что я совсем не знаком с Java и программированием в целом.
Спасибо заранее