get name of app which created the notification - android

I'm working on an android application that listens for notifications. Is there any way to get the name of the app which created the notification.

You need to get the package name first on receiving the new notification from the notification service::
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String ticker = new String();
if(sbn.getNotification().tickerText != null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap id = sbn.getNotification().largeIcon;
}
And then use the "pack" value to get the app name:
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( pack, 0);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ?
pm.getApplicationLabel(ai) : "(unknown)");

Related

PocketSphinx: How to recognize Keyword followed by dynamic word?

I have been working on PocketSphinx Android voice command project where the user can open application listed on their Android by use command "open xxx" (I made "open" as Keyword and xxx is application's name), but sadly it didn't work.
My question is, how PocketSphinx recognize two-words command, where the first word is Keyword and the second word is a dynamic word (user can say any application's name).
Declare keyword:
public class PocketSphinxActivity extends Activity implements
RecognitionListener {
/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "open";
I tried to Split() two-words keyword (ex: open camera) and successfully opened camera, but what I need is "open xxx" so the user can define any application they want. That's why I change keyword to "open" only.
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null)
return;
String text = hypothesis.getHypstr();
String[] split = text.split("\\s+");
String word1 = split[0];
String word2 = split[1];
if (word2 != null) {
if (word1.equals("open")) {
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
int size = packs.size();
boolean uninstallApp = false;
boolean exceptFlg = false;
for (int v = 0; v < size; v++) {
PackageInfo p = packs.get(v);
String tmpAppName = p.applicationInfo.loadLabel(packageManager).toString();
String pname = p.packageName;
tmpAppName = tmpAppName.toLowerCase();
if (tmpAppName.trim().toLowerCase().equals(word2.trim().toLowerCase())) {
PackageManager pm = this.getPackageManager();
Intent appStartIntent = pm.getLaunchIntentForPackage(pname);
if (null != appStartIntent) {
try {
this.startActivity(appStartIntent);
} catch (Exception e) {
}
}
}
}
finish();
public void onResult(Hypothesis hypothesis) {
((TextView) findViewById(R.id.result_text)).setText("");
if (hypothesis != null) {
String text = hypothesis.getHypstr();
String[] split = text.split("\\s+");
String word1 = split[0];
String word2 = split[1];
makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
Any help on this would be grateful. I really need it to complete my final project. Thank you.

How to get another app name with packages/uri?

How can i get the name of the app from the package name. I don't want the name of my app. Here is my code:
String packName ="com.apphousebd.ramadan2017";
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( packName, 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

remove message notification through android code

Through my android app I want to clear/remove message notification (that is generated by default in android device) of an incoming message if it contains keyword. Can anyone please help how can I do that?
use NotificationListenerService api and get notification id and text then clear notification by notification id.
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = "";
String ticker = "";
String title = "";
String text = "";
String postTime = "";
if (sbn.getPackageName() != null)
pack = sbn.getPackageName();
postTime = sbn.getPostTime() + "";
Bundle extras = sbn.getNotification().extras;
CharSequence data = extras.getCharSequence(Notification.EXTRA_TITLE);
extras.getCharSequence(Notification.EXTRA_TITLE);
if (data != null) {// notification title
title = data.toString();
}
data = extras.getCharSequence(Notification.EXTRA_TEXT);
if (data != null) {// notification text
text = data.toString();
}
String id= sbn.getKey() ;// notification id
}

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!");
}

Android App Icon Filemanager

I want to add an option in my file manager to show the App Icons of a directory. The code below didn't work; what did I do wrong?
ImageView icon;
private static Activity activity;
String temp = mFileMang.getCurrentDir();
} else if (sub_ext.equalsIgnoreCase("apk")) {
final Drawable appicon;
try {
PackageInfo packageInfo = activity.getPackageManager()
.getPackageArchiveInfo(temp,
PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo = packageInfo.applicationInfo;
appInfo.sourceDir = temp;
appInfo.publicSourceDir = temp;
appicon = appInfo
.loadIcon(activity.getPackageManager());
mViewHolder.icon.setImageDrawable(appicon);
} catch (Exception e) {
mViewHolder.icon.setImageResource(R.drawable.appicon);
}
try this.. i fetch the icon from the sd card directory ..icon from the apk files which are not installed ...
public class A extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_listing);
ListView list = (ListView) findViewById(R.id.app_listing);
ArrayList<PackageInfoStruct> listData = getApks();
list.setAdapter(new TestAdapter(listData, A.this));
}
class PackageInfoStruct {
String appname = "";
String pname = "";
String versionName = "";
int versionCode = 0;
Drawable icon;
String datadir = "";
}
public ArrayList<PackageInfoStruct> res;
private ArrayList<PackageInfoStruct> getApks() {
try {
String path = Environment.getExternalStorageDirectory() + "/test";
File file = new File(path);
String[] list = file.list();
res = new ArrayList<PackageInfoStruct>();
for (String str : list) {
String not_installed_apk_file = path + "/" + str;
PackageManager pm = getPackageManager();
PackageInfo pi = pm.getPackageArchiveInfo(
not_installed_apk_file, 0);
if (pi == null)
continue;
// the secret are these two lines....
pi.applicationInfo.sourceDir = not_installed_apk_file;
pi.applicationInfo.publicSourceDir = not_installed_apk_file;
//
Drawable APKicon = pi.applicationInfo.loadIcon(pm);
String AppName = (String) pi.applicationInfo.loadLabel(pm);
PackageInfoStruct pack = new PackageInfoStruct();
pack.icon = APKicon;
pack.pname = AppName;
res.add(pack);
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
private ArrayList<PackageInfoStruct> getInstalledApps() {
try {
res = new ArrayList<PackageInfoStruct>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(
0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
PackageInfoStruct newInfo = new PackageInfoStruct();
newInfo.appname = p.applicationInfo.loadLabel(
getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.datadir = p.applicationInfo.dataDir;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(this
.getPackageManager());
res.add(newInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
PackageManager..getPackageArchiveInfo(..) method will work only for installed apks, means it will operate only on an apk file, which has already been installed. Currently you maybe checking this on an apk file, which might not be installed and thus getting package info is failed.
What you can do is:
Unzip the apk file programatically (Basically apk file is a zip file, and you can extract it programatically. see http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically) )
Get the AndroidManifest.xml file from the zip entry
Parse the AndroidManifest.xml file ( see How to parse the AndroidManifest.xml file inside an .apk package )
Get the xml attribute 'android:icon' from it
Read the icon file from the zip entry again as bitmap.
use this icon bitmap wherever you want.
Just Try with this-
ImageView icon;
private static Activity activity;
String temp = mFileMang.getCurrentDir();
} else if (sub_ext.equalsIgnoreCase("apk")) {
final Drawable appicon;
try {
PackageInfo packageInfo = activity.getPackageManager()
.getPackageArchiveInfo(temp,
PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo = packageInfo.applicationInfo;
appInfo.sourceDir = temp;
appInfo.publicSourceDir = temp;
PackageManager pm = getPackageManager();
appicon = pm.getApplicationIcon(appInfo.packageName);
mViewHolder.icon.setImageDrawable(appicon);
} catch (Exception e) {
mViewHolder.icon.setImageResource(R.drawable.appicon);
}
to add this http://stackoverflow.com/questions/17919151/android-app-icon-filemanager/17924795#17924795 to my code i need to cut it. finally i got this:
but when I open the directory it load only 1 icon and show it for all other apps too.
https://www.dropbox.com/s/e2bonh3fkfseggf/Screenshot_2013-07-31-13-58-18.png
File file = new File(temp + "/" + mDataSource.get(position));
} else if (sub_ext.equalsIgnoreCase("apk")) {
try {
Drawable icon = getApk(file);
mViewHolder.icon.setImageDrawable(icon);
} catch (Exception e) {
mViewHolder.icon.setImageResource(R.drawable.appicon);
}
private Drawable getApk(File file2) {
try {
String path = mFileMang.getCurrentDir();
File file = new File(path);
String[] list = file.list();
for (String str : list) {
String not_installed_apk_file = path + "/" + str;
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageArchiveInfo(
not_installed_apk_file, 0);
if (pi == null)
continue;
// the secret are these two lines....
pi.applicationInfo.sourceDir = not_installed_apk_file;
pi.applicationInfo.publicSourceDir = not_installed_apk_file;
//
res = pi.applicationInfo.loadIcon(pm);
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
PackageManager pm = context.getPackageManager();
PackageInfo info =pm.getPackageArchiveInfo(apkPath,PackageManager.GET_ACTIVITIES);

Categories

Resources