create shortcut for thrid-party app, is that possible? - android

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;
}

Related

How to clear old badge count in android

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)

Get original app name without localize

I need get app's names but without local language, only in original language.
I'm from Poland and I need apps names list in original ENGLISH!
I have that code:
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
final List<ResolveInfo> activities = packageManager.queryIntentActivities( intent, 0 );
final int size = activities.size();
for (int i = 0; i < size; i++){
final ResolveInfo info = activities.get(i);
final String label = info.loadLabel(packageManager).toString();
final String pkgName = info.activityInfo.packageName;
final String className = info.activityInfo.name;
Log.d("INFO", label);
}
You can use getResourcesForApplication() from package manager. You can set resource culture and get values.
Please check the code below
PackageManager packageManager = getPackageManager();
final String packageName = "com.android.wallpaper.livepicker";
try {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
if (null != applicationInfo) {
CharSequence label = packageManager.getApplicationLabel(applicationInfo);
Log.d("MyTag", "Default app label is " + label);
Configuration configuration = new Configuration();
configuration.setLocale(new Locale("en"));
Resources resources = packageManager.getResourcesForApplication(packageName);
resources.updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
String localizedLabel = resources.getString(applicationInfo.labelRes);
Log.d("MyTag", "Localized app label is " + localizedLabel);
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("MyTag", "Failed to obtain app info!");
}

Twitter PostActivity issue

This intent has stoppped working . It was working for 2 months
2 month ago class name has changed from "com.twitter.android.PostActivity" to "com.twitter.applib.PostActivity" . I think it changed again. Is it changed again again and again ?
Can anyone help me to post tweet ?
and sorry about my english
try {
getPackageManager().getPackageInfo(
"com.twitter.android", 0);
Intent twitterIntent = new Intent(
Intent.ACTION_VIEW);
String twitterVersionName = getPackageManager()
.getPackageInfo("com.twitter.android",
0).versionName;
VersionControl currentVersion = new VersionControl(
twitterVersionName);
VersionControl requestedVersion = new VersionControl(
"4.1.9");
if (currentVersion.compareTo(requestedVersion) > -1) {
twitterIntent.setClassName(
"com.twitter.android",
"com.twitter.applib.PostActivity");
} else {
twitterIntent.setClassName(
"com.twitter.android",
"com.twitter.android.PostActivity");
}
twitterIntent.putExtra(Intent.EXTRA_TEXT,
tweetText);
startActivity(twitterIntent);
} catch (NameNotFoundException e) {
try {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/intent/tweet?source=webclient&text="
+ URLEncoder.encode(
tweetText, "UTF-8"))));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
Twitter change "com.twitter.applib.PostActivity" to "com.twitter.applib.composer.TextFirstComposerActivity"
You don't need to search for exact name , you can try like this,
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Your text here");
shareIntent.putExtra(Intent.EXTRA_STREAM, screenShot);
String appName = "twitter";
// use also instagram, pinterest, mail etc for appName.(not facebook)
final PackageManager pm = _activity.getPackageManager();
final List<?> activityList = pm.queryIntentActivities(shareIntent, 0);
int len = activityList.size();
Log.d("Tag","Length: "+len);
for (int i = 0; i < len; i++)
{
final ResolveInfo app = (ResolveInfo) activityList.get(i);
Log.d("Apps on share list: "+ app.activityInfo.name);
if ((app.activityInfo.name.contains(appName)))
{
Log.d("Tag","Found package: "+app.activityInfo.name);
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.setComponent(name);
//TODO change your activity here
_activity.startActivityForResult(shareIntent,SHARE_REQUEST_CODE);
return;
}
}
// not found so make default redirecting
Log.d("Tag","Not Found package: ");

create a share intent with respect to sms messages

background
it's very common to share content by using intents:
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "msg");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
final Intent chooserIntent = Intent.createChooser(shareIntent, "Share using...");
startActivity(chooserIntent);
it's even possible to add receipients to the same sharing intent :
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
shareIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, new String[] { phoneNumber });
problem
the problem is the content of the message itself. on SMS, messages can cost money, so it's important to keep them short in order to send only a single message instead of multiple messages.
so you might say , just keep the message short, but i would like to be able to do both - create a fancy message for all apps (maybe add embed images and links), except that if it's for SMS, make it short.
what i've done is to create a dialog with the intents to be used, while for sms i simply set the message to be shorter.
question
how can i do it?
i've tried the next code , but it for some reason it doesn't show the phone number for sms sending.
here's the code:
create and show the dialog:
final String phoneNumber = "12346556", email = "test#gmail.com";
final String smsMessage="sms", message="message", title="title";
final PackageManager pm = getPackageManager();
//
final Uri uri = Uri.parse("smsto:" + phoneNumber);
final Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("sms_body", smsMessage);
smsIntent.putExtra("address", new String[] { phoneNumber });
final List<ResolveInfo> smsResInfo = pm.queryIntentActivities(smsIntent, 0);
final Set<String> smsPackages = new HashSet<String>();
for (final ResolveInfo ri : smsResInfo)
smsPackages.add(ri.activityInfo.packageName + ri.activityInfo.name);
//
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
shareIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, new String[] { phoneNumber });
final List<ResolveInfo> shareResInfo = pm.queryIntentActivities(shareIntent, 0);
//
final List<ResolveInfo> mergedResInfo = new ArrayList<ResolveInfo>(smsResInfo);
for (final ResolveInfo resolveInfo : shareResInfo) {
if (smsPackages.contains(resolveInfo.activityInfo.packageName + resolveInfo.activityInfo.name))
continue;
mergedResInfo.add(resolveInfo);
}
//
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final String[] items = new String[mergedResInfo.size()];
for (int i = 0; i < mergedResInfo.size(); i++) {
final ResolveInfo resolveInfo = mergedResInfo.get(i);
items[i] = resolveInfo.loadLabel(pm).toString();
}
//
final ListAdapter adapter = new ArrayAdapterWithIcon(MainActivity.this, items, mergedResInfo);
builder.setTitle("Select app").setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, final int item) {
final ResolveInfo resolveInfo = mergedResInfo.get(item);
Intent intent;
if (smsPackages.contains(resolveInfo.activityInfo.packageName + resolveInfo.activityInfo.name))
intent = smsIntent;
else
intent = shareIntent;
intent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(intent);
}
}).show();
ArrayAdapterWithIcon class:
public class ArrayAdapterWithIcon extends ArrayAdapter<String> {
private final List<ResolveInfo> mMergedResInfo;
public ArrayAdapterWithIcon(final MainActivity context, final String[] items,
final List<ResolveInfo> mergedResInfo) {
super(context, android.R.layout.select_dialog_item, items);
this.mMergedResInfo = mergedResInfo;
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView textView = (TextView) view.findViewById(android.R.id.text1);
final Drawable icon = mMergedResInfo.get(position).loadIcon(getPackageManager());
textView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
textView.setCompoundDrawablePadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12,
getContext().getResources().getDisplayMetrics()));
return view;
}
}
what can i do in order to fix this code?
ok ,the problem is with those lines:
final Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.putExtra("address", new String[] { phoneNumber });
should be:
final Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("address", phoneNumber);
if you need to send to more numbers, just add "," between them.
i also think the part with the EXTRA_PHONE_NUMBER isn't needed.

How do I start another application (downloaded or preinstalled) from an activity?

Basically, I want to get a list of all installed apps and pick one to run from an activity.
I've tried ACTION_PICK with Intents but that seems to leave out apps that were downloaded and it has a bunch of junk in it.
Thanks
// to get the list of apps you can launch
Intent intent = new Intent(ACTION_MAIN);
intent.addCategory(CATEGORY_LAUNCHER);
List<ResolveInfo> infos = getPackageManager().queryIntentActivities(intent, 0);
// resolveInfo.activityInfo.packageName = packageName
// resolveInfo.activityInfo.name = className
// reusing that intent
intent.setClassName(packageName, className);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent)
Hope that's enough to help you figure out.
final File favFile = new File(Environment.getRootDirectory(), DEFAULT_FAVORITES_PATH);
try {
favReader = new FileReader(favFile);
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "Couldn't find or open favorites file " + favFile);
return;
}//gives the path for downloaded apps in directory
private void loadApplications(boolean isLaunching) {
if (isLaunching && mApplications != null) {
return;
}
PackageManager manager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
if (apps != null) {
final int count = apps.size();
if (mApplications == null) {
mApplications = new ArrayList<ApplicationInfo>(count);
}
mApplications.clear();
for (int i = 0; i < count; i++) {
ApplicationInfo application = new ApplicationInfo();
ResolveInfo info = apps.get(DEFAULT_KEYS_SEARCH_LOCAL);
application.title = info.loadLabel(manager);
application.setActivity(new ComponentName(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name),
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
application.icon = info.activityInfo.loadIcon(manager);
mApplications.add(application);
}
}
This will help u to load all the apps downloaded.

Categories

Resources