I want to get number of total apps in android and make sure that this is not working.
int numberOfInstalledApps = getPackageManager().getInstalledApplications(0).size();
This always returns 138,
If I uninstall a app then returns 138
If I download an app then returns 138.
To get the non-system applications installed on the device, you can do the following :
public static ArrayList<String> getAllInstalledApps() {
ArrayList<String> applicationsList = new ArrayList<>();
List<PackageInfo> packageList = getPackageManager()
.getInstalledPackages(0);
for (int i=0; i < packageList.size(); i++) {
PackageInfo packInfo = packageList.get(i);
if ((packInfo.applicationInfo.flags
& ApplicationInfo.FLAG_SYSTEM) == 0) {
String applicationName = packInfo.applicationInfo
.loadLabel(getPackageManager()).toString();
applicationsList.add(applicationName);
Log.e("App >" + Integer.toString(i), applicationName);
}
}
return applicationsList;
}
You can then get the list by doing:
int number = getInstalledApps().size();
You can also start any of the applications by calling:
Intent callAppIntent = new Intent(Intent.ACTION_MAIN, null);
callAppIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List appsList = context.getPackageManager()
.queryIntentActivities(callAppIntent, 0);
Related
I am trying to execute a function that orders the hours loaded inside a multidimensional array. The function works great in the emulator and in phones with an API less than 20, but when running it on phones with an API greater than 20, the function does not run correctly.
My function:
public int[] ordenarLlaves(String hora[][]){
int indx[] = new int[hora.length];
String aux[] = new String[hora.length];
for(int i = 0; i < hora.length;i++){
if(hora[i][1].length() != 0)
aux[i] = hora[i][1];
}
List<Date> list = convertirHoras(aux);
Collections.sort(list, new Comparator<Date>(){
#Override
public int compare(Date o1, Date o2) {
return o1.compareTo(o2);
}
});
for(int i = 0; i < list.size(); i++){
for (int h = 0; h < aux.length; h++) {
if (list.get(i).compareTo(convertirHora(aux[h])) == 0) {
indx[i] = h;
}
}
}
return indx;
}
Other function that I use:
public Date convertirHora(String hora){
Date hor = new Date();
String aux = "";
try{
SimpleDateFormat formato = new SimpleDateFormat("h:mm a");
for(int c = 0; c < hora.length(); c++){
aux += Character.toString(hora.charAt(c));
if(aux.charAt(c) == 'M'){break;}
}
hor = formato.parse(hora);
}catch(ParseException e){}
return hor;
}
How it works in the android emulator:
In phone (ANDROID 5.0, API 21)
I think, is something with Date class, but I'm not sure... :(
UPDATE: Now, I´m running the app in a cellphone with ANDROID 4.4.2, API 19. Works nice...
I have no doubt that the contains() method is working correctly but apparently I am doing something wrong. I am working on an android app and I am detecting WiFi and listing the WiFi in a listView. I am attempting to filter out duplicate occurrences of the same Access Point so I am creating an array to keep track of duplicates. Here is my code:
public void getWifi(){
ListView wifiList = (ListView)findViewById(R.id.listView);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> apList = wifiManager.getScanResults();
wifiManager.startScan();
List<String> wifees = new ArrayList<>();
//split info into arrays
for(int i=0; i < apList.size(); i++){
String[] daList = String.valueOf(apList.get(i)).split(",");
String info = "";
List<String> SSIDs = new ArrayList<>();
for (String listItem : daList) {
String topic = String.valueOf(listItem.split(":")[0]);
String val = listItem.split(":")[1];
Log.d(TAG, topic);
if (Objects.equals(topic, "SSID")) {
if(SSIDs.contains(val)){
Log.d(TAG, "CONTAINS");
break;
} else {
SSIDs.add(val);
info = val;
}
} else if (Objects.equals(topic, " level")){
String strength = "";
if (Integer.parseInt(val.substring(1)) >= -35) {
strength = "100%";
} else if (Integer.parseInt(val.substring(1)) <= -35 && Integer.parseInt(val.substring(1)) >= -65) {
strength = "50%";
} else {
strength = "1%";
}
info += " " + strength;
}
}
wifees.add(info);
}
assert wifiList != null;
ArrayAdapter apter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, wifees);
wifiList.setAdapter(apter);
}
This is the contents of an apList item or what I get when I call apList.get(i):
SSID: SCHOOL-GUEST, BSSID: 04:04:04:04:04:04, capabilities: [ESS], level: -91, frequency: 2412, timestamp: 72500687089, distance: ?(cm), distanceSd: ?(cm), passpoint: no, ChannelBandwidth: 0, centerFreq0: 0, centerFreq1: 0, 80211mcResponder: is not supported
Here is the output of that code(roughly because it is on a phone and I would rather not include the AP names for security reasons):
SCHOOL-WIRELESS 100%
SCHOOL-SECURE 50%
SCHOOL-GUEST 50%
SCHOOL WIRELESS 1%
SCHOOL_WIRELESS 1%
SCHOOL-SECURE 1%
SCHOOL-GUEST 1%
bxz1872 50%
...
...
...
(so on and so forth)
If I could just get a second opinion that would be great. I feel like I am missing something obvious. Thanks!
.contains doesnt work, because you call List<String> SSIDs = new ArrayList<>(); within for(int i=0; i < apList.size(); i++){. You basically create new empty List for every loop-iteration. You should call it once befor the outer for-loop:
List<String> SSIDs = new ArrayList<>();
for(int i=0; i < apList.size(); i++){
...
}
i have following Problem. I write a little Application , that creates a Java Virtual Machine. If I start this programm inside of Visual Studio it works fine. But if i start it outside of visual studio the programm does not work and i have a ntdll.dll crash.
Here is my Code :
int result = 0;
LoadRuntimeLibrary(libPath);
// Load the JVM library
g_jniLibrary = LoadLibrary(libPath);
if (g_jniLibrary == NULL) {
info->Error("Could not load libary: ");
return -1;
}
// Grab the create VM function address
JNI_createJavaVM createJavaVM = (JNI_createJavaVM)GetProcAddress(g_jniLibrary, "JNI_CreateJavaVM");
if (createJavaVM == NULL) {
info->Error("ERROR: Could not find JNI_CreateJavaVM function");
return -1;
}
// Count the vm args
int numVMArgs = -1;
while (vmArgs[++numVMArgs] != NULL) {}
// Add the options for exit and abort hooks
int numHooks = 0;
JavaVMOption* options = (JavaVMOption*)malloc((numVMArgs + numHooks) * sizeof(JavaVMOption));
for (int i = 0; i < numVMArgs; i++){
options[i].optionString = vmArgs[i];
options[i].extraInfo = 0;
}
// Setup hook pointers
options[numVMArgs].optionString = "abort";
options[numVMArgs].extraInfo = (void*)&AbortHook;
options[numVMArgs + 1].optionString = "exit";
options[numVMArgs + 1].extraInfo = (void*)&ExitHook;
JavaVMInitArgs init_args;
memset(&init_args, 0, sizeof(init_args));
init_args.version = JNI_VERSION_1_8;
init_args.options = options;
init_args.nOptions = numVMArgs + numHooks;
init_args.ignoreUnrecognized = JNI_FALSE;
result = createJavaVM(&jvm, &env, &init_args); // here is the crash
env = GetJNIEnv(false);
Init(env);
result = RunMainClass(env, mainCls, argc, javaargs);
jvm->DestroyJavaVM();
FreeLibrary(g_jniLibrary);
return result;
I hope you hava any idea , what could be wrong
You are accessing the options array out of bounds. It only contains numVMArgs elements, as numHooks is zero.
This of course leads to undefined behavior when you do
options[numVMArgs].optionString = "abort";
options[numVMArgs].extraInfo = (void*)&AbortHook;
options[numVMArgs + 1].optionString = "exit";
options[numVMArgs + 1].extraInfo = (void*)&ExitHook;
as the indexes numVMArgs and numVMArgs + 1 are out of bounds.
I'm trying to make a small utility mod for Minecraft 1.7.10 in which you place a list of IDs in a config file and the mod removes their recipes from the game. I've got the config file working but I don't seem to be able to remove the recipes.
My method for removing the recipes is one I have seen in a couple of forum threads elsewhere and slightly modified to work with my config file (or not work in my case).
Here is the method:
private void removeRecipes(String toDelete)
{
ArrayList<?> recipes = (ArrayList<?>) CraftingManager.getInstance().getRecipeList();
ItemStack recipeResult = null;
ItemStack resultItem = new ItemStack((Item)Item.itemRegistry.getObject(toDelete));
resultItem.stackSize = 1;
resultItem.setItemDamage(0);
for (int scan = 0; scan < recipes.size(); scan++)
{
IRecipe tmpRecipe = (IRecipe) recipes.get(scan);
if (tmpRecipe instanceof ShapedRecipes)
{
ShapedRecipes recipe = (ShapedRecipes)tmpRecipe;
recipeResult = recipe.getRecipeOutput();
recipeResult.stackSize = 1;
recipeResult.setItemDamage(0);
}
if (tmpRecipe instanceof ShapelessRecipes)
{
ShapelessRecipes recipe = (ShapelessRecipes)tmpRecipe;
recipeResult = recipe.getRecipeOutput();
recipeResult.stackSize = 1;
recipeResult.setItemDamage(0);
}
if (ItemStack.areItemStacksEqual(resultItem, recipeResult))
{
System.out.println("[RecipeRemover] Removed Recipe: " + recipes.get(scan) + " -> " + recipeResult);
recipes.remove(scan);
}
}
}
Something was obviously a bit out with my code, but this is a much more efficient approach which works (and should work universally):
#SuppressWarnings("unchecked")
private void removeRecipes(String toDelete)
{
ItemStack resultItem = new ItemStack((Item)Item.itemRegistry.getObject(toDelete));
resultItem.stackSize = 1;
resultItem.setItemDamage(0);
List<IRecipe> recipes = CraftingManager.getInstance().getRecipeList();
for (int i = 0; i < recipes.size(); i++)
{
IRecipe tmpRecipe = recipes.get(i);
ItemStack recipeResult = tmpRecipe.getRecipeOutput();
if(recipeResult != null)
{
recipeResult.stackSize = 1;
recipeResult.setItemDamage(0);
}
if (ItemStack.areItemStacksEqual(resultItem, recipeResult))
{
recipes.remove(i--);
}
}
}
Is there a way I can read a contacts number using the PIM API. I'm using the code below and it's just returning the name. I want the code to return the number only from a contact.
pim = PIM.getInstance();
ContactsList.setModel(new DefaultListModel());
try {
String[] pimListNames = pim.listPIMLists(PIM.CONTACT_LIST);
for (int i = 0; i < pimListNames.length; ++i) {
ContactList cl = (ContactList) pim.openPIMList(
PIM.CONTACT_LIST, PIM.READ_ONLY, pimListNames[i]);
Enumeration items = cl.items();
while (items.hasMoreElements()) {
Contact c = (Contact) items.nextElement();
ContactsList.addItem(c.getString(Contact.FORMATTED_NAME, 0));
}
}
} catch (PIMException ex) {
WittyClassObject.showAlert("error", ex.toString());
}
Have you tried to use
c.getString(Contact.TEL, 0) // instead of c.getString(Contact.FORMATTED_NAME, 0) ?