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和程序设计总体上并不熟悉。
提前谢谢
答案 0 :(得分:0)
简而言之,(有关更多详细信息,请参阅此link)
隐含意图是行动的目标。因此,使用该操作声明的所有广播接收器都将被调用。
明确意图是针对特定组件的意图。因此,只有该特定组件才会收到该意图。在您的情况下,它是Receiver
,您将需要对代码进行少量更改以使其起作用。
在清单文件中声明以下内容。
<receiver android:name=".MainActivity$Receiver">
<intent-filter>
<action android:name="com.example.activity_1"></action>
</intent-filter>
</receiver>
,并在您的点击监听器中发送广播。 您还需要定义特定的组件(Receiver
)以使意图明确。否则,该意图将是隐式的,并且假定您正在使用API 28,它将无法正常工作。
// 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);