如何将我的应用设置为默认短信应用?
How do I set my app as the default SMS app?
我正在关注 this tutorial 将我的应用程序设置为默认短信应用程序,但由于某种原因,我的应用程序没有出现在可用选项列表中。我已尝试尽可能多地对此进行研究,但所有内容都指向同一个教程,或者已过时。我也需要 <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>
为了使您的应用有资格被选为默认消息传递应用(就系统而言),其清单必须列出该博客中描述的四个组件中的每一个 post ,这些组件的 classes 是否实际存在并起作用。 class 名称可以是您喜欢的任何有效名称,但每个组件的其余部分应该与所示的几乎完全相同:
<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>
由于系统只检查应用程序的清单以确定它是否可以充当默认消息传递应用程序,因此您实际上不需要任何这些 classes,尽管您可能必须抑制某些 warnings/errors,或提供存根 classes,让您的 IDE 开心。
显然,如果您的应用要充当用户的默认消息传递客户端,它应该完全实现所有指定的组件。然而,一个不完整的实现肯定是有用的;例如,在学习和测试期间,或在只需要临时部分访问权限的应用程序中,例如消息备份和恢复应用程序。
如果您打算执行任何 SMS/MMS-related 任务,您还需要相关权限。虽然系统在确定符合条件的默认应用候选者时显然不会检查这些,但它们的相应操作需要以下权限:
<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
,尽管有些可能很容易错过;例如,如果 RECEIVE_SMS
权限在系统尝试将传入的 SMS 传递给清单注册的接收者时丢失。如果您观察到意外行为,请务必检查您的日志,即使没有明显的崩溃。
我正在关注 this tutorial 将我的应用程序设置为默认短信应用程序,但由于某种原因,我的应用程序没有出现在可用选项列表中。我已尝试尽可能多地对此进行研究,但所有内容都指向同一个教程,或者已过时。我也需要 <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>
为了使您的应用有资格被选为默认消息传递应用(就系统而言),其清单必须列出该博客中描述的四个组件中的每一个 post ,这些组件的 classes 是否实际存在并起作用。 class 名称可以是您喜欢的任何有效名称,但每个组件的其余部分应该与所示的几乎完全相同:
<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>
由于系统只检查应用程序的清单以确定它是否可以充当默认消息传递应用程序,因此您实际上不需要任何这些 classes,尽管您可能必须抑制某些 warnings/errors,或提供存根 classes,让您的 IDE 开心。
显然,如果您的应用要充当用户的默认消息传递客户端,它应该完全实现所有指定的组件。然而,一个不完整的实现肯定是有用的;例如,在学习和测试期间,或在只需要临时部分访问权限的应用程序中,例如消息备份和恢复应用程序。
如果您打算执行任何 SMS/MMS-related 任务,您还需要相关权限。虽然系统在确定符合条件的默认应用候选者时显然不会检查这些,但它们的相应操作需要以下权限:
<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
,尽管有些可能很容易错过;例如,如果 RECEIVE_SMS
权限在系统尝试将传入的 SMS 传递给清单注册的接收者时丢失。如果您观察到意外行为,请务必检查您的日志,即使没有明显的崩溃。