我编写了一个写入短信存储的短信应用程序。这一直有效,直到 Andorid 4.4 KitKat。然后我读到总是有 1 个主要的短信应用程序。 现在我想在我做我的事情之前询问用户是否想使用我的应用程序作为主应用程序,并在我完成写入存储后要求更改回来。
所以首先我想要一个这样的对话框来让我写:
然后我想将其改回用户之前的状态。
我现在的代码不起作用:
String defaultSmsApp = null;
if (android.os.Build.VERSION.SDK_INT >= 19) {
defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(myContext);
final String myPackageName = getPackageName();
Log.e("SMS Faker", "myPackageName=" + myPackageName);
Log.e("SMS Faker", "defaultSmsApp=" + defaultSmsApp);
if (!defaultSmsApp.equals(myPackageName)) {
Intent intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, myContext.getPackageName());
startActivity(intent);
}
}
最佳答案
应用程序成为短信应用程序的要求是
Implement SMS_DELIVER_ACTION broadcast receiver with BROADCAST_SMS permission.
Implement WAP_PUSH_DELIVER_ACTION broadcast receiver with BROADCAST_WAP_PUSH permission.
Implement RESPOND_VIA_MESSAGE intent and should support smsto Uri scheme and require SEND_RESPOND_VIA_MESSAGE permission.
Implement ACTION_SENDTO intent support smsto Uri scheme.
因此,您的 list 需要如下所示,并且上述所有字段均为必填字段。只有这样你的Intent Intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT);才会起作用
<activity
android:name="MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<receiver
android:name="SmsReciever"
android:permission="android.permission.BROADCAST_SMS" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<receiver
android:name="MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH" >
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<service
android:name="RespondService"
android:exported="true"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
关于android - 在 KitKat 中获取短信访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24129802/