问题
I am trying to get the list of all available launchers on my phone. Clicking on particular launcher from the list should make it a default Home screen launcher. On clicking the default phone launchers (TouchWiz Home in Samsung), the app stops unfortunately (but returns to the home screen which was set earlier). I could only open the third party launchers but it is not being set as the default home screen, and on clicking the home button it goes back to the previous home screen. The code is as below. Is there any possibility to make it work ideally like a pop up list which android gives to choose to launch the homescreen and makes it default? Any help, please? Thank you!
public class AppsListActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apps_list);
loadApps();
loadListView();
addClickListener();
}
private PackageManager manager;
private List<AppDetail> apps;
private void loadApps(){
manager = getPackageManager();
apps = new ArrayList<AppDetail>();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0);
for(ResolveInfo ri:availableActivities){
AppDetail app = new AppDetail();
/* public class AppDetail {
CharSequence label;
CharSequence name;
Drawable icon;}*/
app.label = ri.loadLabel(manager);
app.name = ri.activityInfo.packageName;
app.icon = ri.activityInfo.loadIcon(manager);
apps.add(app);
}
}
private ListView list;
private void loadListView(){
list = (ListView)findViewById(R.id.apps_list);
ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(this,R.layout.list_item,apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.list_item, null);
}
ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
appIcon.setImageDrawable(apps.get(position).icon);
TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
appLabel.setText(apps.get(position).label);
TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
appName.setText(apps.get(position).name);
return convertView;
}
};
list.setAdapter(adapter); }
private void addClickListener() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int pos,long id){
Intent i = manager.getLaunchIntentForPackage(apps.get(pos).name.toString());
ComponentName cn = i.getComponent();
i.setClassName(cn.getPackageName(),cn.getClassName());
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addCategory(Intent.CATEGORY_DEFAULT);
AppsListActivity.this.startActivity(i);
}
});
}
}
回答1:
I instead tried using the default settings app of Android. Can directly launch settings app to choose using below code
Intent i = new Intent(Settings.ACTION_HOME_SETTINGS);
startActivity(i);
来源:https://stackoverflow.com/questions/44030573/unable-to-launch-default-home-screen-from-a-list-of-all-launchers