默认的短信应用选择器未显示。

huangapple go评论68阅读模式
英文:

Default sms app chooser is not displaying

问题

You are missing the code for displaying the SMS app chooser. Here's the code you can add to your MainActivity to display the chooser for setting your app as the default SMS app:

private static final int REQUEST_SET_DEFAULT_SMS_APP = 1;

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

    if (!isDefaultSmsApp(this)) {
        requestSetDefaultSmsApp();
    }
}

private void requestSetDefaultSmsApp() {
    Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
    startActivityForResult(intent, REQUEST_SET_DEFAULT_SMS_APP);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_SET_DEFAULT_SMS_APP) {
        if (isDefaultSmsApp(this)) {
            // Your app is now set as the default SMS app.
        } else {
            // User did not set your app as the default SMS app.
            // Handle this case as needed.
        }
    }
}

This code will request the user to set your app as the default SMS app and handle the result when the user makes a choice. Make sure to declare the REQUEST_SET_DEFAULT_SMS_APP constant in your class.

英文:

I am working on an Sms app, I want my app to display the below chooser, so that the user can select my app as default
默认的短信应用选择器未显示。

here is my manifest code:


    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

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


    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning">

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

        <!-- <service android:name=".BackgroundService" />-->

        <!-- Service that delivers messages from the phone "quick response" -->
        <service
            android:name=".BackgroundService"
            android:exported="false"
            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>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:exported="true">

            <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=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS"
            android:exported="false">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

        <receiver
            android:name=".MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH"
            android:exported="false">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

    </application>

and here is my mainActivity:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (!isDefaultSmsApp(this))
            setDefaultSmsApp();
    }

    private void setDefaultSmsApp() {
        Toast.makeText(this, "Setting default....", Toast.LENGTH_SHORT).show();
        Intent intent =
                new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
                getPackageName());
        startActivity(intent);
    }

    public boolean isDefaultSmsApp(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return context.getPackageName().equals(Telephony.Sms.getDefaultSmsPackage(context));
        }

        return true;
    }

    public void requestSmsPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)) {
            new AlertDialog.Builder(this)
                    .setTitle("Permission Needed")
                    .setMessage("This permission is needed to send messages")
                    .setPositiveButton("Ok", (dialogInterface, i) -> ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, ACCESS_SMS_CODE))
                    .setNegativeButton("Cancel", (dialogInterface, i) -> {
                        dialogInterface.dismiss();
                        finish();
                    })
                    .create()
                    .show();
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, ACCESS_SMS_CODE);
        }

    }

What am i missing ?

I have also tried using intent.createChooser but it was not working, as you can see i have registered all the necessary registers in my manifest,

答案1

得分: 0

After doing some more research I was able to show the dialog here is the updated code for my Main Activity:

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

    if (!isDefaultSmsApp(this))
        setDefaultSmsApp();
}

private void setDefaultSmsApp() {
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.Q){
        //String mypackagename = getPackageName();
        if(Telephony.Sms.getDefaultSmsPackage(this)!=null){
            if (Telephony.Sms.getDefaultSmsPackage(this).equals(getPackageName())){
                openMainActivity();
            }else{
                Intent setSmsAppIntent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,getPackageName());
                startActivityForResult(setSmsAppIntent, 1);
            }
        }else{
            Toast.makeText(this, "No default Sms App found", Toast.LENGTH_SHORT).show();}
    }
    else{
        RoleManager rolemanager = getApplicationContext().getSystemService(RoleManager.class);
        if (rolemanager.isRoleAvailable(RoleManager.ROLE_SMS)){
            if (rolemanager.isRoleHeld(RoleManager.ROLE_SMS)){
                openMainActivity();
            }
            else{
                Intent roleRequestIntent = rolemanager.createRequestRoleIntent(RoleManager.ROLE_SMS);
                startActivityForResult(roleRequestIntent,REQUEST_SET_DEFAULT_SMS_APP);
            }
        }
    }
}

Hope this helps someone!
英文:

After doing some more research I was able to show the dialog here is the updated code for my Main Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_default_sms);
if (!isDefaultSmsApp(this))
setDefaultSmsApp();
}
private void setDefaultSmsApp() {
if(Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.Q){
//String mypackagename = getPackageName();
if(Telephony.Sms.getDefaultSmsPackage(this)!=null){
if (Telephony.Sms.getDefaultSmsPackage(this).equals(getPackageName())){
openMainActivity();
}else{
Intent setSmsAppIntent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,getPackageName());
startActivityForResult(setSmsAppIntent, 1);
}
}else{
Toast.makeText(this, &quot;No default Sms App found&quot;, Toast.LENGTH_SHORT).show();}
}
else{
RoleManager rolemanager = getApplicationContext().getSystemService(RoleManager.class);
if (rolemanager.isRoleAvailable(RoleManager.ROLE_SMS)){
if (rolemanager.isRoleHeld(RoleManager.ROLE_SMS)){
openMainActivity();
}
else{
Intent roleRequestIntent = rolemanager.createRequestRoleIntent(RoleManager.ROLE_SMS);
startActivityForResult(roleRequestIntent,REQUEST_SET_DEFAULT_SMS_APP);
}
}
}
}

Hope this helps someone!

huangapple
  • 本文由 发表于 2023年5月7日 14:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定