如何将字符串传递给SMSReceiver扩展了BroadcastReceiver?

时间:2016-06-08 09:07:32

标签: java android

我想将String传递给我的SMSReceiver。 但我得到了以下错误。

  

无法启动接收器net.learn2develop.LocationTracker.SMSReceiver:java.lang.NullPointerException

这是我想在这里传递字符串的活动:

save = (Button) findViewById(R.id.savebtn);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String p=pass.getText().toString();
            Intent intent = new Intent(LocationTrackerActivity.this, SMSReceiver.class);
            intent.putExtra("string", p);
            sendBroadcast(intent);

        }
    });

在这里我得到了字符串:

 void onReceive(Context context, Intent intent) 
    {        
        //---get the SMS message that was received---
      //Toast.makeText(context, intent.getStringExtra("string"),                 Toast.LENGTH_LONG).show();
    String user_message = intent.getStringExtra("string");
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str="";
    if (bundle != null)
    {
        senderTel = "";

        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            if (i==0) {
                //---get the sender address/phone number---
                senderTel = msgs[i].getOriginatingAddress();
            } 
            //---get the message body---
            str += msgs[i].getMessageBody().toString();                 
        }

        if (str.startsWith(user_message)) {
            //---use the LocationManager class to obtain locations data---
            lm = (LocationManager) 
                    context.getSystemService(Context.LOCATION_SERVICE);    

            //---request location updates---
            locationListener = new MyLocationListener();
            lm.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 
                    60000, 
                    1000, 
                    locationListener);

            //---abort the broadcast; SMS messages won’t be broadcasted---
            this.abortBroadcast();
        }
    }                         
}

我搜索但注意到有帮助。我的尝试得到了这个错误。 有什么建议吗?

0 个答案:

没有答案