无法从所有启动器列表中启动默认主屏幕
Unable to launch default home screen from a list of all launchers
我正在尝试获取 phone 上所有可用启动器的列表。单击列表中的特定启动器应使其成为默认的主屏幕启动器。单击默认 phone 启动器(三星的 TouchWiz Home)时,应用程序不幸停止(但 returns 到之前设置的主屏幕)。我只能打开第三方启动器,但它没有被设置为默认主屏幕,点击主页按钮会返回到之前的主屏幕。代码如下。有没有可能让它像一个弹出列表一样理想地工作,android 可以选择启动主屏幕并将其设为默认?有什么帮助吗?谢谢!
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);
}
});
}
}
我改为尝试使用 Android 的默认设置应用程序。可以使用下面的代码直接启动设置应用程序进行选择
Intent i = new Intent(Settings.ACTION_HOME_SETTINGS);
startActivity(i);
我正在尝试获取 phone 上所有可用启动器的列表。单击列表中的特定启动器应使其成为默认的主屏幕启动器。单击默认 phone 启动器(三星的 TouchWiz Home)时,应用程序不幸停止(但 returns 到之前设置的主屏幕)。我只能打开第三方启动器,但它没有被设置为默认主屏幕,点击主页按钮会返回到之前的主屏幕。代码如下。有没有可能让它像一个弹出列表一样理想地工作,android 可以选择启动主屏幕并将其设为默认?有什么帮助吗?谢谢!
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);
}
});
}
}
我改为尝试使用 Android 的默认设置应用程序。可以使用下面的代码直接启动设置应用程序进行选择
Intent i = new Intent(Settings.ACTION_HOME_SETTINGS);
startActivity(i);