无法在Android Marshmallow中接收短信

时间:2017-07-16 10:53:34

标签: android android-6.0-marshmallow

我无法在Android Marshmallow中播放短信。

清单文件:

<?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.
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您在运行时要求获得许可:

try
{
    // Disable auto commit
    mysqli_autocommit( $db_conn, FALSE );

    $query = "TRUNCATE TABLE `table`;";

    if( ! mysqli_query( $db_conn, $query ) )
    {
        throw new \Exception( "ERROR TRUNCATE: ".$query, 1 );
    }

    foreach( $data as $d )
    {
        $query = "INSERT INTO ....";

        if( ! mysqli_query( $db_conn, $query ) )
        {
            throw new \Exception( "ERROR INSERT: ".$query, 2 );
        }
    }

    // Success finish
    mysqli_commit( $db_conn );
}
catch( \Exception $e )
{
    echo $e->getMessage()."\n";
    echo "errno: " . mysqli_errno( $db_conn ) . PHP_EOL . "\n";
    echo "error: " . mysqli_error( $db_conn ) . PHP_EOL . "\n";
    // Go back to initial status
    mysqli_rollback( $db_conn );
}

而不是你想做什么(如果用户授予权限):

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.
}
}

您可以在此处阅读更多内容:https://developer.android.com/training/permissions/requesting.html