通过Intent

时间:2019-06-10 05:59:43

标签: android

我的AndroidManifest.xml文件

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


<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_SMS">
            <intent-filter>
                <action
                    android:name = "android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>

</receiver>



<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"> </uses-permission>

我的登录类

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

        next = findViewById(R.id.button);
        next.setOnClickListener(this);

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

                        if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.RECEIVE_SMS)){

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

                    }

}

@Override
    public void onRequestPermissionsResult(int requestCode,String permissions[],int[] grantResults){
        switch (requestCode){
            case MY_PERMISSION_REQUEST_RECEIVE_SMS:{
                if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(getApplicationContext(),"Thanks for permitting",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"No permission",Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    @Override
        public void onClick(View v) {

                 Intent intent = new Intent();                          
                 intent.setAction("android.provider.Telephony.SMS_RECEIVED");
                 sendBroadcast(intent);
    }

我的MyReceiver类

package com.example.gofresh;
//all imports
public class MyReceiver extends BroadcastReceiver {

         private static final String TAG = "SmsBroadcastReceiver";

        @Override
            public void onReceive(Context context, Intent intent) {

                    Log.i(TAG,"Intent Received: " +intent.getAction());
        }
    }

我的日志

019-06-10 11:19:39.063 14497-14497/com.example.gofresh D/AndroidRuntime: Shutting down VM
2019-06-10 11:19:39.067 14497-14497/com.example.gofresh E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gofresh, PID: 14497
    java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.provider.Telephony.SMS_RECEIVED from pid=14497, uid=10184
        at android.os.Parcel.createException(Parcel.java:1950)
        at android.os.Parcel.readException(Parcel.java:1918)
        at android.os.Parcel.readException(Parcel.java:1868)
        at android.app.IActivityManager$Stub$Proxy.broadcastIntent(IActivityManager.java:3842)
        at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1012)
        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:444)
        at com.example.gofresh.SignIn.onClick(SignIn.java:122)
        at android.view.View.performClick(View.java:6669)
        at android.view.View.performClickInternal(View.java:6638)
        at android.view.View.access$3100(View.java:789)
        at android.view.View$PerformClick.run(View.java:26145)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6863)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:23397)
        at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:24071)
        at android.app.IActivityManager$Stub.onTransact$broadcastIntent$(IActivityManager.java:10250)
        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:167)
        at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3820)
2019-06-10 11:19:39.088 14497-14569/com.example.gofresh D/OSTracker: OS Event: crash
2019-06-10 11:19:39.104 14497-14497/com.example.gofresh I/Process: Sending signal. PID: 14497 SIG: 9

打开应用程序时,系统会要求我授予许可,并且它会被授予,并且Toast Mesasage也会弹出。但是,当我单击我的按钮时,应用程序崩溃。 我只剪切了我称为广播接收器的那部分代码。 即使我在SignIn类的onClick方法中注释掉了手动意图部分,它仍然在MyReceiver类中显示了我已经调用过onReceive方法,即使我在代码中没有其他地方调用过它。我觉得无论哪里出错,都与我对这部分的误解密切相关。

2 个答案:

答案 0 :(得分:0)

您必须添加权限Sms 在应用级清单文件中的清单标签中添加以下权限

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

如果您现在在清单中添加此权限,请注意应用程序级别的gradle文件中的targetSDKVersion(如果它大于22),则必须提供权限运行时

答案 1 :(得分:0)

当您发送广播时,手机上的每个应用程序都会接收该广播。除非是LocalBroadcast。 现在,您正在发送系统广播“ android.provider.Telephony.SMS_RECEIVED”,该广播可以由许多其他应用程序注册。为了安全起见,此广播仅由SYSTEM发送。希望您能了解崩溃的原因。