Using AlarmManager I can set alarm for any time from the android App. But is there any way to list all the alarms set by me. What should be my approach towards that, as AlarmManager do not provide with such methods. Should I go for saving the alarm as a file in the memory.?
Please Help me with this.
Visiting all the possible links I came to a solution that creating an app that would retrieve the alarms set on the OS level is not possible. Ya.. to some extent it would be possible but in many cases it would be machine dependent.
So better option is to save your Alarms in your database.
A. There is a good explanation on this post regarding this. There are no guarantees that the AlarmClock app will be on every device your app is installed on. For example, many of the HTC phones replace it with HTC's own "World Clock" app.
However, assuming the stock AlarmClock app is present, you should be able to get a cursor from its content provider. See this project as an example.
B. You have to create a layout for items of the ListView.
You can find tutorials about this on Internet : http://www.vogella.com/articles/AndroidListView/article.html http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/
c.
final String tag_alarm = "tag_alarm";
Uri uri = Uri.parse("content://com.android.alarmclock/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());
}
}
Related
I'm making an Android launcher as an introduction to making Android apps for myself, and part of my design requires me to know how many apps are installed on a user's device, and preferably count only the ones which are normal apps that can be launched. I wish to store this number of apps in a global, integer variable. With only this goal in mind, what is the simplest way of just retrieving this number as that variable?
You could use the getInstalledApplications() method of PackageManager.
From the documentation:
Return a List of all application packages that are installed on the
device. If flag GET_UNINSTALLED_PACKAGES has been set, a list of all
applications including those deleted with DONT_DELETE_DATA (partially
installed apps with data directory) will be returned.
Something like this should work:
int numberOfInstalledApps = getPackageManager(0).getInstalledApplications().size();
To filter out the system apps you could do something like this:
int numberOfNonSystemApps = 0;
List<ApplicationInfo> appList = getPackageManager().getInstalledApplications(0);
for(ApplicationInfo info : appList) {
if((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
numberOfNonSystemApps++;
}
}
The all non-system apps have a launch Intent so you just need to fetch the list of all apps and check, how many of them has a launch intent or if not then that app will be a system app.The list of all apps can easily be retrieved by package manager and then we go through the information of all apps while looking for the available launch intent.
As suggested by Darshan Patel : #Brad Larson♦
PackageManager pm = getPackageManager();
int nonSysAppsCount=0;
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo:packages){
if( pm.getLaunchIntentForPackage(packageInfo.packageName) != null ){
String currAppName = pm.getApplicationLabel(packageInfo).toString();
nonSysAppsCount++;
//This app is a non-system app
}
else{
//System App
}
}
If you are looking for the non-system applications installed on a given device, you can do the following :
public static ArrayList<String> getInstalledApps() {
ArrayList<String> appList = new ArrayList<>();
List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
for (int i=0; i < packList.size(); i++) {
PackageInfo packInfo = packList.get(i);
if ( (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
appList.add(appName);
Log.e("App >" + Integer.toString(i), appName);
}
}
return appList;
}
So, you can the list by doing :
int number = getInstalledApps().size();
Additionally, you can start any of the applications by calling :
Intent myIntent = new Intent(Intent.ACTION_MAIN, null);
myIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List appsList = context.getPackageManager().queryIntentActivities(myIntent, 0);
I'm trying to toggle todo items in IBM Connections to complete/incomplete using the SBT Java API.
I manage to set the todo item to complete, but how do I change it back to incomplete?
todoNode = activityService.getActivityNode( "856b9450-b3d2-4b41-a198-46feeb3772a8" );
System.out.println("Title " + todoNode.getTitle());
if ( todoNode.getCategoryFlagCompleted() == null) {
List<String> flags = new java.util.ArrayList();
flags.add("Completed");
todoNode.setFlags(flags);
}
activityService.updateActivityNode(todoNode);
Many thanks
From Connections REST API documentation:
To complete an activity, add this flag. If it is not present, the activity is not completed.
So, to mark an activity as incomplete again just update the ActivityNode without adding the "Completed" flag.
todoNode = activityService.getActivityNode( "856b9450-b3d2-4b41-a198-46feeb3772a8" );
System.out.println("Title " + todoNode.getTitle());
if ( todoNode.getCategoryFlagCompleted() != null) {
todoNode.setFlags(new java.util.ArrayList());
}
activityService.updateActivityNode(todoNode);
Just ran into same problem, however it seems you can use empty flag to get it to work.
todoNode = activityService.getActivityNode( "856b9450-b3d2-4b41-a198-46feeb3772a8" );
System.out.println("Title " + todoNode.getTitle());
if ( todoNode.getCategoryFlagCompleted() == null) {
List<String> flags = new java.util.ArrayList();
flags.add("");
todoNode.setFlags(flags);
}
activityService.updateActivityNode(todoNode);
Not sure if it works in Java tho, cause i use API in JSSS.
What's more, this solution will delete other flags like "Deleted".
You should check for them using getCategoryFlagDelete() to recreate activity "flag field" properly.
I am struggling for a couple of hours now on how to link a discid to a musicbrainz mbid.
So, using dietmar-steiner / JMBDiscId
JMBDiscId discId = new JMBDiscId();
if (discId.init(PropertyFinder.getProperty("libdiscid.path")))
{
String musicBrainzDiscID = discId.getDiscId(PropertyFinder.getProperty("cdrom.path"));
}
or musicbrainzws2-java
Disc controller = new Disc();
String drive = PropertyFinder.getProperty("cdrom.path");
try {
DiscWs2 disc =controller.lookUp(drive);
log.info("DISC: " + disc.getDiscId() + " match: " + disc.getReleases().size() + " releases");
....
I can extract a discid for freedb or musicbrainz easily (more or less), but I have not found a way on calculating the id I that I need to download cover art via the CoverArtArchiveClient from last.fm.
CoverArtArchiveClient client = new DefaultCoverArtArchiveClient();
try
{
UUID mbid = UUID.fromString("mbid to locate release");
fm.last.musicbrainz.coverart.CoverArt coverArt = client.getByMbid(mbid);
Theoretically, I assume, I could you the data collected by musicbrainzws2-java to trigger a search, and then use the mbid from the result ... but that cannot be the best option to do.
I am happy about any push into the right direction...
Cheers,
Ed.
You don't calculate the MBID. The MBID is attached on every entity you retrieve from MusicBrainz.
When getting releases by DiscID you get a list. Each entry is a release and has an MBID, accessible with getId():
for (ReleaseWs2 rel : disc.getReleases()){
log.info("MBID: " + rel.getId() + ", String: " + rel.toString());
}
You then probably want to try the CoverArtArchive (CAA) for every release and take the first cover art you get.
Unfortunately I don't know of any API documentation for musicbrainzws2 on the web. I recommend running javadoc on all source files.
I am working on an android app used to access a Box account. The problem I am facing is how to determine a folder/file in the user's account is read only (shared with him/her as a Viewer) so that the upload/delete operations can be disabled.
What I currently do is:
1) Get the items in a folder:
BoxCollection itemsCollection = _boxClient.getFoldersManager()
.getFolderItems(folderId, folderContentRequest);
String userMail = ...
ArrayList<BoxTypedObject> result = null;
2) Determine which one is folder, get it's collaborations, check if it's accessible by the logged-in user, and check whether he is an editor:
if (itemsCollection != null) {
result = itemsCollection.getEntries();
for(BoxTypedObject boxObject : result) {
if(boxObject instanceof BoxAndroidFolder) {
BoxAndroidFolder folder = (BoxAndroidFolder)boxObject;
List<BoxCollaboration> folderCollaborations = _boxClient.getFoldersManager().getFolderCollaborations(folder.getId(), null);
for(BoxCollaboration collaboration : folderCollaborations) {
if( userMail.equalsIgnoreCase(collaboration.getAccessibleBy().getLogin()) &&
!BoxCollaborationRole.EDITOR.equalsIgnoreCase(collaboration.getRole()))
System.out.println("" + folder.getName() + " is readonly");
}
}
}
}
So, is there a simpler and faster (fewer requests) way to get that property of a folder with the android SDK?
You can first check the owner of the folder (folder.getOwnedBy()), if it's the current user then you don't need to check collaborations. However if it's not the current user you'll have to check collaborations.
I know it's possible to get the local device name as described in the solution to this question Display Android Bluetooth Device Name
What I'm interested in knowing is, can I change the local buetooth name (the one other devices see when I'm in discovery mode) programaticlly. I know you can change it by hand, but I'm writing and app and I want to be able to change the name (add a simple flag) so other devices with the same application can scan and instantly know if the phone is also running the app.
tl;dr: How can I change the bluetooth device name on android?
Yes you can change your device name using setName(String name) of BluetoothAdapter type.Following is the sample code:
private BluetoothAdapter bluetoothAdapter = null;
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
void ChangeDeviceName(){
Log.i(LOG, "localdevicename : "+bluetoothAdapter.getName()+" localdeviceAddress : "+bluetoothAdapter.getAddress());
bluetoothAdapter.setName("NewDeviceName");
Log.i(LOG, "localdevicename : "+bluetoothAdapter.getName()+" localdeviceAddress : "+bluetoothAdapter.getAddress());
}
Thanks for the original answer, here are a few things I found when implementing that might help someone else out.
1) BT has to be enabled for setName() to work.
2) It takes time for BT to Enable. ie. you Can't just call enable() then setName()
3) It takes time for the name to "sink in". ie. you can't call getName() right after setName() and expect the new name.
So, here is a snippet of code I came up with to use a runnable to get the job done in the background. It is also time bound to 10seconds, so it won't run forever if there is a problem.
Finally, this is part of our power on check, and we normally leave BT disabled (due to battery). So, I turn BT back off after, you may not want to do that.
// BT Rename
//
final String sNewName = "Syntactics";
final BluetoothAdapter myBTAdapter = BluetoothAdapter.getDefaultAdapter();
final long lTimeToGiveUp_ms = System.currentTimeMillis() + 10000;
if (myBTAdapter != null)
{
String sOldName = myBTAdapter.getName();
if (sOldName.equalsIgnoreCase(sNewName) == false)
{
final Handler myTimerHandler = new Handler();
myBTAdapter.enable();
myTimerHandler.postDelayed(
new Runnable()
{
#Override
public void run()
{
if (myBTAdapter.isEnabled())
{
myBTAdapter.setName(sNewName);
if (sNewName.equalsIgnoreCase(myBTAdapter.getName()))
{
Log.i(TAG_MODULE, "Updated BT Name to " + myBTAdapter.getName());
myBTAdapter.disable();
}
}
if ((sNewName.equalsIgnoreCase(myBTAdapter.getName()) == false) && (System.currentTimeMillis() < lTimeToGiveUp_ms))
{
myTimerHandler.postDelayed(this, 500);
if (myBTAdapter.isEnabled())
Log.i(TAG_MODULE, "Update BT Name: waiting on BT Enable");
else
Log.i(TAG_MODULE, "Update BT Name: waiting for Name (" + sNewName + ") to set in");
}
}
} , 500);
}
}
To change the bluetooth name properly you need to take care of following things:
1) You need following permissions:
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
2) Check the bluetooth state from adapter as you can only change the name of bluetooth is turned on.
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter.state == BluetoothAdapter.STATE_ON){
bluetoothAdapter.setName("NewDeviceName");
}
3) If the bluetooth is not turned on then you can turn it on with the following command:
bluetoothAdapter.enable()
4) Last thing, please don't use static timers to wait for bluetooth state changes instead the proper way is that you can register for android.bluetooth.adapter.action.STATE_CHANGED broadcast and useBluetoothAdapter.EXTRA_STATE to get the new state of bluetooth whenever it is changed.
Note: Not all devices behave the same when it comes to bluetooth and changing the name due to caching and hw address, so never expect same outcome from all devices.