2016-03-01 43 views
0

我试图在屏幕上显示传入的短信。但是当我发送新短信时没有任何事情发生。Android - API 23 - 无法显示传入的短信

我正在使用Nexus 5(Api 23)模拟器。

当调试我:

许可拒绝:接收意图{ACT = android.provider.Telephony.SMS_RECEIVED FLG = 0x8000010(具有额外)}需要由于发送者com.android android.permission.RECEIVE_SMS .phone

In MainActivity.Java我处理权限。

@TargetApi(Build.VERSION_CODES.M) 
public void getPermissionToReceiveSMS() { 

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) 
      != PackageManager.PERMISSION_GRANTED) { 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (shouldShowRequestPermissionRationale(Manifest.permission.RECEIVE_SMS)) { 
       // Show our own UI to explain to the user why we need to read the contacts 
       // before actually requesting the permission and showing the default UI 
      } 
     } 

     requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS},REQUEST_RECEIVE_SMS); 
    } 
} 

我在做什么错? 你能帮我吗?

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.globa8track.gtw"> 

    <uses-permission android:name="android.permission.READ_SMS" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <receiver android:name=".gtwWidget"> 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/gtw_widget_info" /> 
     </receiver> 

     <receiver android:name=".smsReceiver"> 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 

     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action." /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

smsReceiver.Java

package com.globa8track.gtw; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.util.Log; 
import android.widget.Toast; 


public class smsReceiver extends BroadcastReceiver { 

    public static final String TAG = "Listener: incoming SMS."; 

    @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(); 
      if (bundle != null){ 
       Object[] pdus = (Object[]) bundle.get("pdus"); 
       SmsMessage[] messages = new SmsMessage[pdus.length]; 
       for (int i = 0; i < pdus.length; i++){ 
        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
       } 
       for (SmsMessage message : messages){ 

        String strMessageFrom = message.getDisplayOriginatingAddress(); 
        String strMessageBody = message.getDisplayMessageBody(); 

        Toast.makeText(context, "SMS Message received from:" +strMessageFrom, Toast.LENGTH_LONG).show(); 
        Toast.makeText(context, "SMS Message content" +strMessageBody, Toast.LENGTH_LONG).show(); 

        //smsReceiver.class.getSmsDetails(strMessageFrom, strMessageBody); 

       } 
      } 
     } 

    } 
} 

MainActivity.Java

package com.globa8track.gtw; 

import android.Manifest; 
import android.annotation.TargetApi; 
import android.content.pm.PackageManager; 
import android.os.Build; 
import android.support.annotation.NonNull; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    /** 
    * Id to identify a read sms permission request 
    * 
    */ 
    private static final int REQUEST_READ_SMS = 1; 
    private static final int REQUEST_READ_PHONE_STATE = 2; 
    private static final int REQUEST_RECEIVE_SMS = 3; 

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

     getPermissionToToast(); 
     getPermissionToReadSMSinbox(); 
     getPermissionToReceiveSMS(); 
    } 

    // Called when the user is performing an action which requires the app to read the 
    // sms inbox 
    @TargetApi(Build.VERSION_CODES.M) 
    public void getPermissionToReadSMSinbox() { 
     // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid 
     // checking the build version since Context.checkSelfPermission(...) is only available 
     // in Marshmallow 
     // 2) Always check for permission (even if permission has already been granted) 
     // since the user can revoke permissions at any time through Settings 
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) 
       != PackageManager.PERMISSION_GRANTED) { 

      // The permission is NOT already granted. 
      // Check if the user has been asked about this permission already and denied 
      // it. If so, we want to give more explanation about why the permission is needed. 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       if (shouldShowRequestPermissionRationale(Manifest.permission.READ_SMS)) { 
        // Show our own UI to explain to the user why we need to read the contacts 
        // before actually requesting the permission and showing the default UI 
       } 
      } 

      // Fire off an async request to actually get the permission 
      // This will show the standard permission request dialog UI 
      requestPermissions(new String[]{Manifest.permission.READ_SMS},REQUEST_READ_SMS); 
     } 
    } 


    @TargetApi(Build.VERSION_CODES.M) 
    public void getPermissionToToast() { 

     if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) 
       != PackageManager.PERMISSION_GRANTED) { 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       if (shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE)) { 
        // Show our own UI to explain to the user why we need to read the contacts 
        // before actually requesting the permission and showing the default UI 
       } 
      } 

      requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},REQUEST_READ_PHONE_STATE); 
     } 
    } 

    @TargetApi(Build.VERSION_CODES.M) 
    public void getPermissionToReceiveSMS() { 

     if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) 
       != PackageManager.PERMISSION_GRANTED) { 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       if (shouldShowRequestPermissionRationale(Manifest.permission.RECEIVE_SMS)) { 
        // Show our own UI to explain to the user why we need to read the contacts 
        // before actually requesting the permission and showing the default UI 
       } 
      } 

      requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS},REQUEST_RECEIVE_SMS); 
     } 
    } 



    // Callback with the request from calling requestPermissions(...) 
    @Override 
    public void onRequestPermissionsResult(int requestCode, 
              @NonNull String permissions[], 
              @NonNull int[] grantResults) { 
     // Make sure it's our original READ_CONTACTS request 
     if (requestCode == REQUEST_READ_SMS) { 
      if (grantResults.length == 1 && 
        grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       Toast.makeText(this, "READ_SMS permission granted", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(this, "READ_SMS permission denied", Toast.LENGTH_SHORT).show(); 
      } 
     } else { 
      super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     } 


     if(requestCode == REQUEST_READ_PHONE_STATE){ 
      if (grantResults.length == 1 && 
        grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       Toast.makeText(this, "READ_PHONE_STATE permission granted", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(this, "READ_PHONE_STATE permission denied", Toast.LENGTH_SHORT).show(); 
      } 

     }else{ 
      super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     } 


     if(requestCode == REQUEST_RECEIVE_SMS){ 
      if (grantResults.length == 1 && 
        grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       Toast.makeText(this, "RECEIVE_SMS permission granted", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(this, "RECEIVE_SMS permission denied", Toast.LENGTH_SHORT).show(); 
      } 

     }else{ 
      super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     } 
    } 


} 
+0

删除'Toasts'并使用其他内容,如'Log'语句或断点。 – CommonsWare

+0

@CommonsWare tks的答复,但为什么? Toast在Api23中不起作用? – ThelmaJay

+1

'吐司'很好。这是一个糟糕的调试工具,我试图避免像'BroadcastReceiver'的'onReceive()'这样的地方。 – CommonsWare

回答

0

我缺少的AndroidManifest.xml的主要内活动代码。

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity>