我是android开发的新手,所以我需要你的帮助。我已经在互联网上搜索过,但找不到解决方案,所以最后我决定在这里发帖......因为我是新手,请尽可能给我更多信息。
这就是我正在做的...
我的活动测试按钮很少,当用户长按按钮时,它会打开
(MAIN_ACTIVITY) 已安装的所有应用程序列表。当用户选择一个应用程序时,它会获得
相应的包名称并返回到 TEST 活动。现在,当用户单击该按钮时,它将打开新应用程序(在返回 TEST 活动之前已选择该应用程序)。
在快捷方式中,用户应该能够动态编辑按钮(应用链接、图标、标题)。
现在这是我的问题...
我已经能够实现上述目标,但它仅适用于 1 个按钮。我的意思是其他按钮做同样的事情。这让我发疯......如果你们需要 xml 代码,请告诉我。提前谢谢各位。
测试活动
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ListView;
public class Test extends Activity implements OnLongClickListener,
OnClickListener {
final int APPLIST_REQUEST_CODE = 12345;
final int APPLIST_REQUEST_CODE2 = 2;
String pac, pac2;
private ListView mListAppInfo;
Button bBrowser, bCalculator, bExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
bBrowser = (Button) findViewById(R.id.bBrowser);
bExit = (Button) findViewById(R.id.bExit);
bCalculator = (Button) findViewById(R.id.bCalculator);
bBrowser.setOnClickListener(this);
bBrowser.setOnLongClickListener(this);
bCalculator.setOnClickListener(this);
bCalculator.setOnLongClickListener(this);
bExit.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bBrowser:
Intent i = getPackageManager().getLaunchIntentForPackage(pac);
startActivity(i);
break;
case R.id.bCalculator:
Intent i1 = getPackageManager().getLaunchIntentForPackage(pac2);
startActivity(i1);
break;
case R.id.bExit:
finish();
break;
}
}
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bBrowser:
Intent a = new Intent("android.intent.action.APP_LIST");
startActivityForResult(a, APPLIST_REQUEST_CODE);
break;
case R.id.bCalculator:
Intent a2 = new Intent("android.intent.action.APP_LIST");
startActivityForResult(a2, APPLIST_REQUEST_CODE2);
break;
}
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == APPLIST_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
pac = data.getStringExtra("key");
}
} else if (requestCode == APPLIST_REQUEST_CODE2) {
if (resultCode == Activity.RESULT_OK) {
pac2 = data.getStringExtra("key");
}
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
}
}
主要活动
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView mListAppInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
mListAppInfo = (ListView) findViewById(R.id.lvApps);
AppInfoAdapter adapter = new AppInfoAdapter(this,
Utilities.getInstalledApplication(this), getPackageManager());
mListAppInfo.setAdapter(adapter);
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
AppInfoAdapter appInfoAdapter = (AppInfoAdapter) parent
.getAdapter();
ApplicationInfo appInfo = (ApplicationInfo) appInfoAdapter
.getItem(pos);
Intent a = new Intent();
a.putExtra("key", appInfo.packageName);
setResult(Activity.RESULT_OK, a);
finish();
}
});
}
}