2017-07-16 67 views
-1

我無法在Android棉花糖中烘烤短信。無法在Android棉花糖中收到短信

清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.asim.smsreceive"> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 

     <activity android:name="com.example.asim.smsreceive.MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <!-- BroadcastReceiver that listens for incoming SMS messages --> 
     <receiver 
      android:name=".SmsReceiver" 
      android:enabled="true" 
      android:exported="true" > 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

這是廣播接收器的代碼無法接收短信:

package com.example.asim.smsreceive; 
    public class SmsReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 

      if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){ 
       Bundle bundle = intent.getExtras();   //---get the SMS message passed in--- 
       SmsMessage[] msgs = null; 
       String msg_from; 
       if (bundle != null){ 
        //---retrieve the SMS message received--- 
        try{ 
         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]); 
          msg_from = msgs[i].getOriginatingAddress(); 
          String msgBody = msgs[i].getMessageBody(); 
          Toast.makeText(context,msgBody,Toast.LENGTH_LONG).show(); 
         } 
        }catch(Exception e){ 
    //       Log.d("Exception caught",e.getMessage()); 
        } 
       } 
      } 
     } 
    } 

主要活動

public class MainActivity extends AppCompatActivity { 

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


    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     // Here, thisActivity is the current activity 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.RECEIVE_SMS) 
       != PackageManager.PERMISSION_GRANTED) { 

      // Should we show an explanation? 
       // No explanation needed, we can request the permission. 

       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.RECEIVE_SMS}, 
         1); 

       // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
       // app-defined int constant. The callback method gets the 
       // result of the request. 
     } 
    } 
} 
+0

https://stackoverflow.com/questions/19500381/broadcast -receiver-not-working-for-sms這可能有些幫助 – ik024

+0

我還注意到你還沒有在你的manifest.xxml文件中爲reciver標籤添加任何優先級。嘗試添加它。 – ik024

+0

現在我添加了

回答

1

你必須要求在運行時的權限:

if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) { 


if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.READ_EXTERNAL_STORAGE)) { 

    // Show an explanation to the user *asynchronously* -- don't block 
    // this thread waiting for the user's response! After the user 
    // sees the explanation, try again to request the permission. 

} else { 

    // No explanation needed, we can request the permission. 

    ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
      REQUEST_PERMISSION); 

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
    // app-defined int constant. The callback method gets the 
    // result of the request. 
} 
} 

,比你什麼都想要(如果用戶授予權限):

@Override 
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if (requestCode == REQUEST_PERMISSION) { 
     if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      // Permission granted. 
     } else { 
      // User refused to grant permission. 
     } 
    } 
    } 

你可以在這裏閱讀更多:https://developer.android.com/training/permissions/requesting.html

+0

我已經在運行時要求權限 –