Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:在第二个活动中提取IMEI编号会产生空字符串_Java_Android - Fatal编程技术网

Java Android:在第二个活动中提取IMEI编号会产生空字符串

Java Android:在第二个活动中提取IMEI编号会产生空字符串,java,android,Java,Android,我有两个活动,Main活动和ReceiveActivity(接收器)。我正在尝试检索我的IMEI号码 在第二个活动中(我不能在这里使用意图),但我什么也得不到?这是相关的主活动代码: public String deviceID() { TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); return tm.getDeviceID; } 在接受活动中,我

我有两个活动,Main活动和ReceiveActivity(接收器)。我正在尝试检索我的IMEI号码 在第二个活动中(我不能在这里使用意图),但我什么也得不到?这是相关的主活动代码:

  public String deviceID() {
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    return tm.getDeviceID;
 }
在接受活动中,我只有:

  private Context context;
  MainActivity ps2 = new MainActivity();
  try {
     Toast.makeText(context, "IMEI: " + ps2.deviceID,    Toast.LENGTH_LONG).show();
  } catch (Exception e) {
    e.printstacktrace;
  }
我从来没有得到过祝酒词,大概是因为ps2.deviceID是一个空字符串(或者可能是错误的“上下文”)?谁能 请帮忙?PS:我是在真实的设备上测试这个,而不是在模拟器上;我已经在舱单上看到了状态。 我甚至尝试过以下代码(从这里的一篇旧文章&将上下文更改为服务),这也会使应用程序崩溃:

     public class ReceiveActivity extends BroadcastReceiver {
     @Override

     public void onReceive(Context context, Intent intent) {
     TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
         Toast.makeText(context, "IMEI: " + tMgr.getDeviceID(), Toast.LENGTH_LONG).show();
         ...
我终于成功地让下面的代码工作了(intent filter PHONE_状态丢失),Toast消息现在是正确的,但只要我分配了 用tmgr.getDeviceID()对应用程序崩溃进行编号!这很奇怪。以下是新代码:

 public class SMSReceiver extends BroadcastReceiver {
 String number;

 @Override
 public void onReceive(Context context, Intent intent) {
 try {
    tmgr = (TelephonyManager)  context.getSystemService(Context.TELEPHONY_SERVICE);
    Toast.makeText(context, "IMEI: " + tmgr.getDeviceId(), Toast.LENGTH_LONG).show();
    number = (tmgr.getDeviceId()).trim();   //--The app crashes at this line --
 } catch (Exception e) {
    e.printStackTrace();
 }

为什么你不能使用意图?由于您需要broadcastreceiver中的IMEI,您可以使用包含设备IMEI的customAction和extras从MainActivity发送广播,您还需要在AndroidManifest中为ReceiveActivity指定customAction。此外,“第二个活动”一词的使用也会产生误导,因为您的第二个活动,即ReceiveActivity不是活动类,但清单中的BroadcastReceiver如下所示:
因此当SMS到达时,不会调用MainActivity in。还有:
你的问题我不清楚。但是,如果您想在BroadcastReceiver类中显示设备IMEI,那么您可以使用与在activity中相同的方式来显示设备IMEI,唯一的更改是对于getSystemService,您必须编写context.getSystemService,上下文是BroadcastReceiver的onReceive方法的参数之一。如果你看上面的最后两行,这正是我所做的,它使应用程序崩溃了!我知道在某个地方这是一个上下文问题,但还没有任何东西起作用。我还将
(Service.TELEPHONY\u Service)
更改为
(Context.TELEPHONY\u Service)
,但仍然没有乐趣。这应该存在于您的BroadcastReceiver onreceive
电话管理器tMgr=(TelephonyManager)上下文中。getSystemService(Context.TELEPHONY\u Service)这将像我在BroadcastReceiever类中使用的那样工作