I've implemented a way to get notifications using NotificationContentProvider. This is the code i'm using
private static final Uri NOTIF_COUNT_CONTENT_URI = Uri.parse("content://com.android.tv.notifications.NotificationContentProvider/notifications/count");
private static final String COLUMN_COUNT = "count";
...
private int count;
Cursor c = context.getContentResolver().query(NOTIF_COUNT_CONTENT_URI, null, null, null, null);
if (c != null && c.moveToFirst()) {
int index = data.getColumnIndex(COLUMN_COUNT);
count = data.getInt(index);
}
if (c != null) {
c.close();
}
The problem I'm having an exception
java.lang.SecurityException: App does not have permission: android.permission.ACCESS_NOTIFICATIONS
I've tried adding this permission in manifest and asking in runtime but none of this work. How can I get notification count? I also need to get unseen notifications but I haven't found a way to do it
In terms of Android TV, I think you are going to need to be a system app (move the install directory from /data/app to /system/priv-app) and to set up a priv-app permission file in /etc/permissions whose contents look like:
<permissions>
<privapp-permissions package="your app package name">
<permission name="android.permission.ACCESS_NOTIFICATIONS"/>
</privapp-permissions>
</permissions>
Related
I'm building a Custom Control (a Gallery Picture Picker)
basically trying to re-make a this : Microsoft.Maui.Media.MediaPicker.PickPhotoAsync()
but i didn't find anyway to retrieve user pictures on MAUI ..
on Xamarin.Android i used this code :
var uriExternal = MediaStore.Images.Media.ExternalContentUri;
string[] projection = { (MediaStore.Images.Media.InterfaceConsts.Id),
(MediaStore.Images.Media.InterfaceConsts.DateAdded),
(MediaStore.Images.Media.InterfaceConsts.RelativePath) };
var cursor = Android.App.Application.Context.ContentResolver.Query(uriExternal, projection, null, null, MediaStore.Images.Media.InterfaceConsts.DateAdded);
if (cursor != null) {
int columnIndexID = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.Id);
int RelativePathID = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.RelativePath);
while (cursor.MoveToNext()) {
long imageId = cursor.GetLong(columnIndexID);
string rPath = cursor.GetString(RelativePathID);
//Get the Picture's URI
Android.Net.Uri uriImage = Android.Net.Uri.WithAppendedPath(uriExternal, "" + imageId);
...
}
}
with this Code, given the required permissions, i could get the external path of all pictures of an Android phone to then use them in my PicturePicking custom control ..
==> how can i do this in Microsoft MAUI !! (targeting Android & IOS)
(just to be clear, of course i know that i could just use MediaPicker's [PickPhotoAsync] function, but i clearly don't want that, it's not an option)
Thanks in advance for any help !
So after 2 days of searching.. i've came to the conclusion that the only way to do this is by using Plateform Specific Code ..
(i tryed it successfully)
i am trying to retrieve alarm information from content provider using following code
final String tag_alarm = "tag_alarm";
Uri uri = Uri.parse("content://com.android.deskclock/alarm")
Cursor c = getContentResolver().query(uri, null, null, null, null);
Log.i(tag_alarm, "no of records are" + c.getCount());
Log.i(tag_alarm, "no of columns are" + c.getColumnCount());
if (c != null) {
String names[] = c.getColumnNames();
for (String temp : names) {
System.out.println(temp);
}
if (c.moveToFirst()) {
do {
for (int j = 0; j < c.getColumnCount(); j++) {
Log.i(tag_alarm, c.getColumnName(j);
+ " which has value " + c.getString(j));
}
} while (c.moveToNext());
}
}
it is giving me error permission denial i copied this code from curious answer from query Get alarm infomation
in the Nguyen's comment he pointed a solution "If i embed this code in Android source code and run image file, it can pass "permission denied" error and retrieve alarm information. Anyway, thanks your tip :) " how to embed a code in android source code and run image file ?? please explain i always create a project in eclipse and then code and run it as run application.please explain this trick
In my opinion, because of each manufacturer had implemented their own Clock App,
So default AlarmClockApp of Android will be replaced depend on each manufacturer, that makes your code can not be run successful if Android Os had been modified by Manufacturers.
So I think we can not handle all the devices in this case, instead of that, we should handler it by manufacturer of devices.
With Samsung devices, it's ClockPackage and in androidManifest :
<provider
android:name=".alarm.AlarmProvider"
android:authorities="com.samsung.sec.android.clockpackage"
android:exported="true"
android:readPermission="com.sec.android.app.clockpackage.permission.READ_ALARM"
android:writePermission="com.sec.android.app.clockpackage.permission.WRITE_ALARM" >
</provider>
So we can read data of alarm in Samsung devices by :
add permission in manifest:
<uses-permission android:name="com.sec.android.app.clockpackage.permission.READ_ALARM" />
then get Uri by below :
Uri uri = Uri.parse("content://com.samsung.sec.android.clockpackage/alarm");
Use Uri :
Cursor c = getContentResolver().query(uri, null, null, null, null);
if (c == null) { // that mean devices is not belong to Samsung manufacturer,
// we should use an other uri (don't for get to add permission)
AlarmLog.w("Can not read cursor");
}
AlarmLog.i(tag_alarm, "no of records are " + c.getCount());
AlarmLog.i(tag_alarm, "no of columns are " + c.getColumnCount());
if (c != null) {
String names[] = c.getColumnNames();
for (String temp : names) {
AlarmLog.d(tag_alarm, temp);
}
if (c.moveToFirst()) {
do {
for (int j = 0; j < c.getColumnCount(); j++) {
AlarmLog.i(tag_alarm, c.getColumnName(j)
+ " which has value " + c.getString(j));
}
} while (c.moveToNext());
}
}
Hope it's helpful and receive code for others manufactures.
Look at the definition of the content provider in AndroidManifest.xml
<provider android:name="AlarmProvider"
android:authorities="com.android.deskclock"
android:exported="false" />
The exported is false, which means a 3rd-party app cannot access it. Permission denial as a result.
how to embed a code in android source code and run image file
It means you modify the Android source(Provided by google). I don't think it's useful in your case.
You can do that in a rooted device, by directly modify the contents in sqlite database. I don't think there is a solution to work on all existing Android platforms.
In general, sqlite database files are under /data/data/app-package-name/databases/database-name, so in this example, it should be /data/data/com.android.deskclock/databases/com.android.deskclock or something similar. You can pull the file out by adb pull and open it using SqliteExplorer to check whether it is what you want.
For how to modify this db file, check Using your own SQLite database in Android applications
As said there's no way to do this without root, but you can monitor when the next alarm is and when the value changes with the following value:
Settings.System.getUriFor(Settings.System.NEXT_ALARM_FORMATTED).toString()
This will give you a string with the next alarm.
I want to get all the browser history records from different browsers in android mobile phone ?(Maybe as you know, there are usually more than one browser apps in a phone ).Is there anyone has done this successfully and give me a hint ? Example code is preferred.Any help will be helpful.. Thanks a lot in advance ! :)
Just look at this. I used it in my code and I am getting browser history through it(Default Browser).
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String selection = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
mCursor = this.managedQuery(Browser.BOOKMARKS_URI, proj, selection, null, null);
this.startManagingCursor(mCursor);
mCursor.moveToFirst();
String title = "";
String url = "";
if (mCursor.moveToFirst() && mCursor.getCount() > 0) {
while (mCursor.isAfterLast() == false && cont) {
title = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCursor.moveToNext();
}
}
Hope this helps you.
Limitations :
Browser.BOOKMARKS_URI will, at most, work for the open source Browser app that is part of the Android Open Source Project. Device manufacturers are welcome to replace that app with something else that will not be recording its history, bookmarks, or anything else in that ContentProvider. Similarly, users are allowed to download third-party browsers, which may not be storing things in that ContentProvider.
I have an app on the market, but in some cases it force closes at the open. According to the crash error there is something wrong with the indexOf command (NullPointerException). Part of the code:
contactName = null;
Context context = getApplicationContext();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
myArr.add("");
while (cursor.moveToNext())
{
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
spaceIndex = contactName.indexOf(' '); //this is the bad row
spaceLastIndex = contactName.lastIndexOf(' ');
myArr.add(contactName);
}
I test my app on 3 different phones, app is working fine. So I cannot test the code if I change something, as I could not tell the difference. What can cause the error and why does it emerge on only a few phones? (5% of the downloads). A guy contacted me with this error, there are a couple of contacts with special characters in his phone (HTC Legend CM 7.1, and a Vodafone 845 Android 2.1). So I added the same characters to a contact of mine, put blank spaces before the name, tried everything to mess with the contact name, app runs smoothly, so this is not the problem. I am out of the options.
You should check the contactName, if it isn't null. I think that the column, from which are you trying to get the value, is just empty.
if (contactName != null) { ... and so on
contactName is clearly coming back null. You need some sort of null check before you call indexOf on it.
// snip
while (cursor.moveToNext())
{
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (contactName == null) continue;
spaceIndex = contactName.indexOf(' ');
spaceLastIndex = contactName.lastIndexOf(' ');
myArr.add(contactName);
}
contactName = cursor.getString(cursor.getColumnIndexorThrow(ContactsContract.Contacts.DISPLAY_NAME));
Even the display name can be null for some cases... say blank contact..
so check whether the contactName is null or not first before getting index from it.
can i get url what i write on android device browser..
please tell me..
thanks.
You can acces Browsing history the same way you do that for other ContentProviders. Besides browsing history you can also get list of Bookmarks. HISTORY_PROJECTION_BOOKMARK_INDEX is used to distinguish among them. You need permission com.android.browser.permission.READ_HISTORY_BOOKMARKS to execute this code.
Cursor webLinksCursor = getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, Browser.BookmarkColumns.DATE + " DESC");
int row_count = webLinksCursor.getCount();
int title_column_index = webLinksCursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
int url_column_index = webLinksCursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
if ((title_column_index > -1) && (url_column_index > -1) && (row_count > 0))
{
webLinksCursor.moveToFirst();
while (webLinksCursor.isAfterLast() == false)
{
if (webLinksCursor.getInt(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) != 1)
{
if (!webLinksCursor.isNull(url_column_index))
{
Log.i("History" , "Last page browsed " + webLinksCursor.getString(url_column_index));
break;
}
}
webLinksCursor.moveToNext();
}
}
webLinksCursor.close();
My instinct tells me this is akin to reading the history, I can't see google allowing apps to access this data, it would be abused by malicious applications. However, if someone proves me wrong I'll be chosing my apps far more selectively in future!!