我正在遵循有关将应用程序设置为默认SMS应用程序的教程,但是由于某种原因,我的应用不会出现在可用选项列表中。我试图尽可能多地研究这一点,但是一切都指向同一教程,或者已经过时了。我也需要<receiver>
吗?有人可以解释我做错了什么吗?
代码:
@Override
protected void onResume()
{
super.onResume();
Log.i("MainAcitvity", "On Resume Called");
// Only do these checks/changes on KitKat+, the "mSetDefaultSmsLayout" has its visibility
// set to "gone" in the xml layout so it won't show at all on earlier Android versions.
final String myPackageName = getPackageName();
if (Utility.hasKitKat())
{
if (Utility.isDefaultSmsApp(this))
{
// This app is the default, remove the "make this app the default" layout and
// enable message sending components.
mSetDefaultSmsLayout.setVisibility(View.GONE);
}
else
{
Log.i("MainActivity", "Not Default App");
// Not the default, show the "make this app the default" layout and disable
// message sending components.
mSetDefaultSmsLayout.setVisibility(View.VISIBLE);
Button button = (Button) findViewById(R.id.set_default_sms_button);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
Log.i("MainActivity", "Button Pushed");
//Utility.setDefaultSmsApp(MainActivity.this);
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName);
startActivity(intent);
}
});
}
}
}
清单:
<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" />
<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>
,为了使您的应用被选为默认消息应用程序(就系统而言),其清单必须列出四个组件中的每个组件中的每个组件中的每个组件中的每个组件中的每个组件博客文章,这些组件的课程是否实际存在和功能。类名称可以是您喜欢的任何有效名称,但是每个组件的其余部分都应该完全如图所示:
<manifest>
...
<application>
<!-- BroadcastReceiver that listens for incoming SMS messages -->
<receiver
android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<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>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity android:name=".ComposeSmsActivity">
<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>
<!-- Service that delivers messages from the phone "quick response" -->
<service
android:name=".HeadlessSmsSendService"
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>
</application>
</manifest>
由于系统仅检查应用程序的清单即可确定它是否可以充当默认消息传递应用程序,因此您实际上不需要这些类,尽管您可能必须抑制某些警告/错误,或者提供固执类,让您的IDE开心。
显然,如果您的应用程序要充当用户的默认消息客户端,则应完全实现所有指定的组件。但是,不完整的实施肯定会很有用。例如,在学习和测试期间,或在仅需要部分访问的应用程序中,例如消息备份和还原应用程序。
如果您打算执行任何与SMS/MMS相关的任务,则还需要相关的权限。尽管在确定合格的默认应用程序候选时,该系统显然没有检查这些内容,但其相应操作需要以下权限:
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
如果您错过了给定操作的相关许可,则会抛出SecurityException
,尽管有些操作可能很容易错过;例如,如果系统试图将传入的SMS传递到明显注册的接收器时,则丢失了RECEIVE_SMS
权限。如果您观察到意外行为,即使没有明显的崩溃,请确保检查日志。