I set "0" means its showing badge count was "1". how to clear my old badge count.
//Badge count set method
public static void setBadge(Context mContext, int count) {
String launcherClassName = getLauncherClassName(mContext);
if (launcherClassName == null) {
return;
}
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", mContext.getPackageName());
intent.putExtra("badge_count_class_name", launcherClassName);
mContext.sendBroadcast(intent);
}
//Find out our app and here return app package name.
public static String getLauncherClassName(Context mContext) {
PackageManager pm = mContext.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
if (pkgName.equalsIgnoreCase(mContext.getPackageName())) {
String className = resolveInfo.activityInfo.name;
return className;
}
}
return null;
}
This can be done if using the https://github.com/leolin310148/ShortcutBadger library by calling the method as follows:
ShortcutBadger.applyCount(Context context, int badgeCount)
//or
ShortcutBadger.removeCount(Context context)
Related
I try to start activity from a watchface or a app android.
I able to start the view to choose watchface when user tap on watchface :
#Override
public void onTapCommand(
#TapType int tapType, int x, int y, long eventTime) {
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
I want start other activities like Google Agenda, Google fit, or other ...
It's possible to list all intents available on the device ?
Thanks
This will return all of the launcher activities on a device:
public static List<ResolveInfo> gatherApps(Context context) {
if(allApps == null) {
final PackageManager packageManager = context.getPackageManager();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> infoList = context.getPackageManager().queryIntentActivities(mainIntent, 0);
Collections.sort(infoList, new Comparator<ResolveInfo>() {
#Override
public int compare(ResolveInfo lhs, ResolveInfo rhs) {
return lhs.loadLabel(packageManager).toString().compareTo(rhs.loadLabel(packageManager).toString());
}
});
allApps = infoList;
}
return allApps;
}
I use code as follow to share image to twitter but when display screen of twitter app to input text and image,it only display text ,image don't display.
public void shareImageByTwitter(Context mContext, String path) {
File myFile = new File(path);
final String[] twitterApps = {
// package // name - nb installs (thousands)
"com.twitter.android", "com.twidroid",
"com.handmark.tweetcaster", "com.thedeck.android" };
Intent tweetIntent = new Intent();
tweetIntent.setType("image/jpeg");
tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myFile));
final PackageManager packageManager = mContext.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(
tweetIntent,0);
for (int i = 0; i < twitterApps.length; i++) {
for (ResolveInfo resolveInfo : list) {
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(twitterApps[i])) {
tweetIntent.setPackage(p);
}
}
}
mContext.startActivity(tweetIntent);
}
How must I do.
I use code below to create shortcut for my own app, and I wonder if I can create a shortcut for third-party app? If that's possible, Where could I get the icon(Parcelable)?
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra("duplicate", false);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent(new ComponentName(className, activity)));
context.sendBroadcast(shortcut);
public static void createShortcutForPackage(Context context, String packageName, String className) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, className));
PackageManager pm = context.getPackageManager();
ResolveInfo ri = pm.resolveActivity(intent, 0);
String shortcutName = ri.loadLabel(pm).toString();
String activityName = ri.activityInfo.name;
int iconId = ri.activityInfo.applicationInfo.icon;
Context pkgContext = PackageUtil.createPackageContext(context, packageName);
if (pkgContext != null) {
ShortcutIconResource sir = Intent.ShortcutIconResource.fromContext(pkgContext, iconId);
installShortcut(pkgContext, packageName, activityName, shortcutName, sir);
}
}
public static void installShortcut(Context context, String packageName, String componentName, String shortcutName, Parcelable icon) {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
ComponentName cn = new ComponentName(packageName, componentName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(cn));
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcut.putExtra("duplicate", false);
context.sendBroadcast(shortcut);
}
public static Context createPackageContext(Context context, String pkgName) {
Context result = null;
try {
result = context.createPackageContext(pkgName, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
} catch (NameNotFoundException e) {
Log.d(TAG, "createPackageContext(): " + e.getStackTrace());
}
return result;
}
How can I check that? Do i have to use PackageManager? Thank you :D
I check it this way (check if the returned value is unequal to null):
public static Intent findFacebookClient(Context con)
{
final String[] FacebookApps = {"com.facebook.android", "com.facebook.katana"};
Intent facebookIntent = new Intent();
facebookIntent.setType("text/plain");
final PackageManager packageManager = con.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(facebookIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < FacebookApps.length; i++)
{
for (ResolveInfo resolveInfo : list)
{
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(FacebookApps[i]))
{
facebookIntent.setPackage(p);
return facebookIntent;
}
}
}
return null;
}
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package : " + packageInfo.packageName);
}
Then you can check if packageInfo.packageName is equal to some string which contains the package name of that application.
I have a custom listPreference I would like to display a list of apps that can be launched (contain an activity with CATEGORY_LAUNCHER). The selection will be used later to launch the application. When I did a search for the solution, the list also contained apps that could not be launched. Is there any way to narrow this down?
public class AppSelectorPreference extends ListPreference {
#Override
public int findIndexOfValue(String value) {
return 0;
//return super.findIndexOfValue(value);
}
public AppSelectorPreference(Context context, AttributeSet attrs) {
super(context,attrs);
PackageManager pm = context.getPackageManager();
List<PackageInfo> appListInfo = pm.getInstalledPackages(0);
CharSequence[] entries = new CharSequence[appListInfo.size()];
CharSequence[] entryValues = new CharSequence[appListInfo.size()];
try {
int i = 0;
for (PackageInfo p : appListInfo) {
if (p.applicationInfo.uid > 10000) {
entries[i] = p.applicationInfo.loadLabel(pm).toString();
entryValues[i] = p.applicationInfo.packageName.toString();
i++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
setEntries(entries);
setEntryValues(entryValues);
}
}
Solved:
final Context context = getBaseContext();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
CharSequence[] entries = new CharSequence[pkgAppsList.size()];
CharSequence[] entryValues = new CharSequence[pkgAppsList.size()];
int i = 0;
for ( ResolveInfo P : pkgAppsList ) {
entryValues[i] = (CharSequence) P.getClass().getName();
entries[i] = P.loadLabel(context.getPackageManager());
++i;
};
#Frazerm63 i think you are missing this thing in Your Code
Intent localIntent = new Intent("android.intent.action.MAIN", null);
localIntent.addCategory("android.intent.category.LAUNCHER");
List localList = localPackageManager.queryIntentActivities(localIntent, 0);
Collections.sort(localList, new ResolveInfo.DisplayNameComparator(localPackageManager));
you have to pass your PackageManager object in above code .means this localPackageManager
i have not much idea how you can use this in user Code but this will help to get you idea to filter only some category application.