0

我是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();

        }
    });
}

}
4

1 回答 1

1

首先,您的活动转换在逻辑上是错误的。阅读活动和结果MainActivity服务Test,所以Test应该从MainActivity结果开始,并且MainActivity应该将该结果返回给Testvia onActivityResult。你正在做的是:

Test -> new MainActivity -> new Test

虽然它应该是:

Test -> new MainActivity -> back to the old Test

关于按钮问题,两者都bCalculatorbBrowser同样的事情,这应该是启动在MainActivity. 两个按钮在 中做完全相同的事情onClick,那么为什么它们会有不同的行为呢?

编辑:

Test中,这样调用MainActivity

final int APPLIST_REQUEST_CODE = 12345;
Intent a = new Intent("android.intent.action.APP_LIST");
startActivityForResult(a, APPLIST_REQUEST_CODE);

MainActivity中,以这种方式返回结果:

Intent a = new Intent();
a.putExtra("key", appInfo.packageName);
setResult(Activity.RESULT_OK, a);
finish();

回到Test,你会得到这样的结果:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == APPLIST_REQUEST_CODE) {
        if(resultCode == Activity.RESULT_OK) {
            String packageName = data.getStringExtra("key", "");
        }
    }
}

编辑 2:获取并显示包图标:

将图标资源 ID 和包名称添加到结果意图中:

a.putExtra("icon", appInfo.icon);
a.putExtra("packageName", appInfo.packageName);

以这种方式获取图标:

int iconID = data.getIntExtra("icon", -1);
Drawable icon = getApplicationContext().getPackageManager().getResourcesForApplication(data.getStringExtra("packageName", "")).getDrawable(iconID);
bCalculator.setBackground(icon);
于 2013-07-04T19:49:14.983 回答