others-how to solve java.lang.SecurityException: Failed to find provider sms for user 0; expected to find a valid ContentProvider for this authority error when trying to access content provider in android system?

1. Purpose

In this post, I would demonstrate how to solve SecurityException: Failed to find provider sms for user 0; expected to find a valid ContentProvider for this authority when trying to access android content provider:

Caused by java.lang.SecurityException: Failed to find provider sms for user 0; expected to find a valid ContentProvider for this authority
       at android.os.Parcel.createExceptionOrNull(Parcel.java:2387)
       at android.os.Parcel.createException(Parcel.java:2371)
       at android.os.Parcel.readException(Parcel.java:2354)
       at android.os.Parcel.readException(Parcel.java:2296)
       at android.content.IContentService$Stub$Proxy.registerContentObserver(IContentService.java:1229)
       at android.content.ContentResolver.registerContentObserver(ContentResolver.java:2742)
       at android.content.ContentResolver.registerContentObserver(ContentResolver.java:2730)
       at com.myapp.utils.b2.a(:58)
       at com.myapp.MyService.onStartCommand(:41)
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4680)
       at android.app.ActivityThread.access$2000(ActivityThread.java:269)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2204)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:257)
       at android.app.ActivityThread.main(ActivityThread.java:8218)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1016)



2. Solution

First, we need to create a ContentProvider in your app:

public class MyContentProvider extends ContentProvider {

    @Override
    public boolean onCreate() {
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection,
                        @Nullable String selection,
                        @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection,
                      @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values,
                      @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

}

Then add a provider in your AndroidManifest.xml: (suppose your app’s package name is com.bswen.app)

        <provider
            android:name=".MyContentProvider"
            android:enabled="true"
            android:authorities="com.bswen.app"
            android:exported="false"
            android:grantUriPermissions="true" />



3. The theory

A content provider manages access to a central repository of data. You implement a provider as one or more classes in an Android application, along with elements in the manifest file. One of your classes implements a subclass ContentProvider, which is the interface between your provider and other applications. Although content providers are meant to make data available to other applications, you may of course have activities in your application that allow the user to query and modify the data managed by your provider.

A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.

A provider usually has a single authority, which serves as its Android-internal name. To avoid conflicts with other providers, you should use Internet domain ownership (in reverse) as the basis of your provider authority. Because this recommendation is also true for Android package names, you can define your provider authority as an extension of the name of the package containing the provider. For example, if your Android package name is com.example., you should give your provider the authority com.example..provider.

You can refer to this document to get more info.

3. Summary

In this post, I demonstrated how to solve the java.lang.SecurityException: Failed to find provider sms for user 0; expected to find a valid ContentProvider for this authority when trying to access android content provider in android. That’s it, thanks for your reading.

-->