How to implement a ContentObserver for call logs

Here is the answer. Dont forget to register the content observer with this method: registerContentObserver (Uri uri, boolean notifyForDescendents, ContentObserver observer) And then you can create it like this. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getApplicationContext() .getContentResolver() .registerContentObserver( android.provider.CallLog.Calls.CONTENT_URI, true, new MyContentObserver(handler)); } class MyContentObserver extends ContentObserver { public MyContentObserver(Handler h) { super(h); } @Override … Read more

Android KitKat (API 19) – How to write messages in SMS Content Provider, without sending them, from Non-Default App?

The SmsWriteOpUtils class uses reflection to access methods of the AppOpsManager Service in order to enable/disable a non-default SMS app’s write access to the SMS Provider in API Level 19 (KitKat). Once set, an app’s access mode will be retained until it is reset, or the app is uninstalled. Enabling an app’s write access allows … Read more

Create and Share a File from Internal Storage

It is possible to expose a file stored in your apps private directory via a ContentProvider. Here is some example code I made showing how to create a content provider that can do this. Manifest <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.example.providertest” android:versionCode=”1″ android:versionName=”1.0″> <uses-sdk android:minSdkVersion=”11″ android:targetSdkVersion=”15″ /> <application android:label=”@string/app_name” android:icon=”@drawable/ic_launcher” android:theme=”@style/AppTheme”> <activity android:name=”.MainActivity” android:label=”@string/app_name”> <intent-filter> <action android:name=”android.intent.action.MAIN” /> … Read more

How to get camera result as a uri in data folder?

There are two ways to solve this problem. 1. Save bitmap which you received from onActivityResult method You can start camera through intent to capture photo using below code Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); After capture photo, you will get bitmap in onActivityResult method if (requestCode == CAMERA_REQUEST) { Bitmap photo = (Bitmap) data.getExtras().get(“data”); } … Read more

CursorLoader usage without ContentProvider

I wrote a simple CursorLoader that does not need a content provider: import android.content.Context; import android.database.Cursor; import android.support.v4.content.AsyncTaskLoader; /** * Used to write apps that run on platforms prior to Android 3.0. When running * on Android 3.0 or above, this implementation is still used; it does not try * to switch to the framework’s … Read more

How to use support FileProvider for sharing content to other apps?

Using FileProvider from support library you have to manually grant and revoke permissions(at runtime) for other apps to read specific Uri. Use Context.grantUriPermission and Context.revokeUriPermission methods. For example: //grant permision for app with package “packegeName”, eg. before starting other app via intent context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); //revoke permisions context.revokeUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); As a … Read more