getLaunchIntentForPackage is null for some apps
To create a home screen-style launcher, don't look for apps and then try to get launch Intents
for each. Look for launchable activities, using queryIntentActivities()
on PackageManager
.
For example, this activity (from this sample project) implements a home screen-style launcher using this technique:
/***
Copyright (c) 2008-2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
http://commonsware.com/Android
*/
package com.commonsware.android.launchalot;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
public class Launchalot extends ListActivity {
AppAdapter adapter=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PackageManager pm=getPackageManager();
Intent main=new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);
Collections.sort(launchables,
new ResolveInfo.DisplayNameComparator(pm));
adapter=new AppAdapter(pm, launchables);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
}
class AppAdapter extends ArrayAdapter<ResolveInfo> {
private PackageManager pm=null;
AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
super(Launchalot.this, R.layout.row, apps);
this.pm=pm;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
if (convertView==null) {
convertView=newView(parent);
}
bindView(position, convertView);
return(convertView);
}
private View newView(ViewGroup parent) {
return(getLayoutInflater().inflate(R.layout.row, parent, false));
}
private void bindView(int position, View row) {
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(getItem(position).loadLabel(pm));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
icon.setImageDrawable(getItem(position).loadIcon(pm));
}
}
}
On an Android TV device, you should also search for LEANBACK_LAUNCHER
activities, as that's what Android TV uses, and TV-specific APKs might not have a regular LAUNCHER
activity, or at best have one that is not necessarily ideal for use on a TV.
getLaunchIntentForPackage is null for apps
I have solved the problem:
String s = mArrayProducts.get(position).getPackageName();
Intent PackageManagerIntent = getPackageManager().getLaunchIntentForPackage(s); NULL
startActivity(PackageManagerIntent);
Android Intent To Open Application Not Working
As you can read here, getLaunchIntentForPackage (String packageName)
Return a "good" intent to launch a front-door activity in a package [...]
The current implementation will look first
for a main activity in the category CATEGORY_INFO, next for a main
activity in the category CATEGORY_LAUNCHER, or return null if neither
are found.
so, in the intent, the category will already be set. If you manually change it, probably you are breaking the intent, since the category could not be the correct one that the manager found.
so, just remove the line
i.addCategory(Intent.CATEGORY_LAUNCHER);
Starting ANY of the installed apps
How can i get the launchIntent for dialer and contacts?
Those are not apps. Those are other launchable activities of another app.
Hence, you need to decide what it is that you are writing.
You said that you have "a list of all installed apps and he can pick any app and starts it". In that case, you specifically do not want "dialer and contacts", as those are not apps.
If, instead, you want to show a list of all launchable activities, from which the user can pick, you would not be using getLaunchIntentForPackage()
. Instead, you would use queryIntentActivities()
to find those launchable activities. I have a sample app that demonstrates this, in the form of a launcher.
String mismatch error regarding packageManager and getLaunchIntentForPackage in Kotlin
The error you are receiving is because when you access app.appPackageName
there is a chance that the application's package name may be null. This is because this method is originally written in Java and Kotlin protects itself in case of the value null. That is why it's type is String?.
To overcome this issue, you need to add a nullability check before accessing this value. That way, Kotlin will interpret it's type as String.
override fun getView(position:Int, convertView:View, parent:ViewGroup):View
{
val v:View
if (convertView == null)
{
v = inflater.inflate(R.layout.item_app, parent, false)
}
else
{
v = convertView
}
val myLayoutView = v.findViewById(R.id.layout) as LinearLayout
val myImageView = v.findViewById(R.id.image) as ImageView
val myLabelView =v.findViewById(R.id.label) as TextView
val app = getItem(position) as AppObject
myLabelView.text = app.appName
myImageView.setImageDrawable(app.appImage)
myLayoutView.setOnClickListener(object: View.OnClickListener
{
override fun onClick(v:View)
{
val applicationPackageName : String? = app.appPackageName
if (applicationPackageName != null) {
val launchAppIntent = context.packageManager.getLaunchIntentForPackage(applicationPackageName)
if (launchAppIntent != null)
{
context.startActivity(launchAppIntent)
}
}
} else {
//YOUR LOGIC
}
})
return v
}
getPackageManager() always return null
the Toast shows Error (my else condition),meaning getPackageManager() returns null
No. You would be getting a NullPointerException
in that case. In your case, getLaunchIntentForPackage()
is returning null
.
That, plus your other symptoms, suggests that you do not have an application whose applicationId
is com.b_app
installed on this device.
Related Topics
Android: Detect Softkeyboard Open
Android Change Text Color of Items in Spinner
How to Implement a Gcm Hello World for Android Using Android Studio
Android Ble Bluetoothgatt.Writedescriptor() Return Sometimes False
Custom Translucent Android Actionbar
Live Character Count for Edittext
Android Disable Screen Timeout While App Is Running
Change Expandable Indicator in Expandablelistview
Loading an Android Resource into a Webview
Should Use "Sp" Instead of "Dp" for Text Sizes
Android Support Multidex Library Implementation
Extracting Information from a Scanned Gs1-Type Barcode
Populating Spinner Directly in the Layout Xml