我正试图在屏幕上显示传入的短信。但是当我发送新的短信时,什么也没发生。
我正在使用Nexus 5(Api 23)模拟器。
调试时我得到了:
权限拒绝:接收Intent {act = android.provider.Telephony.SMS_RECEIVED flg = 0x8000010(有额外内容)}需要android.permission.RECEIVE_SMS,因为发件人com.android.phone
在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 :(得分:0)
我在AndroidManifest.xml上缺少MAIN内部活动标记。
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>