0

I'm new to android developing, so guys i need your help. I've searched all over the internet but couldn't find the solution, so finally i decided to post here... Since I'm a newbie please give me more info as you can.

Here is what i'm doing...

I've few buttons on activity TEST, when the user long presses the buttons it will opens the

(MAIN_ACTIVITY)list of all apps installed. And when the user selects an app, it gets the

respective package name and returns to TEST activity. Now when the user clicks the button it will open the new app(which was selected earliear before returning to TEST activity).

In shortcut user should have the ability to edit the button dynamically like(app link,icon,title).

Now here is what my problem is...

I've abled to achieve the above but it only works on 1 button only. I mean other buttons do the same thing. And this is driving me crazy... If you guys need xml code, please let me know. Thanks in advance guys.

TEST ACTIVITY

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();
}
}

MAIN ACTIVITY

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

        }
    });
}

}
user2551070
  • 71
  • 2
  • 11

1 Answers1

1

First of all, your activitiy transitions are logically wrong. Read about activities and results. MainActivity serves Test, so Test should start MainActivity for result, and MainActivity should return that result to Test via onActivityResult. What you are doing instead is:

Test -> new MainActivity -> new Test

While it should be:

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

About the buttons issue, both bCalculator and bBrowser do the same, which should be starting the package selected in MainActivity. Both buttons do the exact same thing in onClick, so why should they have different behavior?

Edit:

In Test, call MainActivity this way:

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

In MainActivity, return the result this way:

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

And back in Test, you get the result this way:

@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", "");
        }
    }
}

Edit 2: Getting and showing the package icon:

Add the icon resource ID and the package name to the result intent:

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

Get the icon this way:

int iconID = data.getIntExtra("icon", -1);
Drawable icon = getApplicationContext().getPackageManager().getResourcesForApplication(data.getStringExtra("packageName", "")).getDrawable(iconID);
bCalculator.setBackground(icon);
Jong
  • 9,045
  • 3
  • 34
  • 66
  • First of all thanks for the reply Jong. Your answer makes sense, so I shouldn't be using BUNDLE to retrieve the package name, correct me if I'm wrong. I should use onActivityResult. so how can i get the package name when an app is selected in main_activity & returns the result(package name) to test_activity via onActivityResult.? With the help of the link mentioned by you, I got the concept but not fully understand what to write. Can you give me some info with code or some tutorial links. Thanks – user2551070 Jul 05 '13 at 05:56
  • Hey Jong, I'm getting an error on onActivityResult it says "The method getString(String,String) is undefined for the type Intent. Where am i doing mistake..? And also what to write on bBrowser & bCalculator on click listener so that they can open distinct app. – user2551070 Jul 05 '13 at 15:16
  • I confused it, `getString` is for `Bundle`. `getStringExtra` is for `Intent`. If you want `bBrowser` to open the browser, it should have the browser package name... And `bCalculator` should have the package name of the calculator. – Jong Jul 05 '13 at 18:34
  • wow what a coincidence buddy, I just found the same thing with the help of this link http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result. Your code just works fine, thanks for that. By the way I can start an activity on bBrowser and bCalculator by proving package name.But the thing is I want to let the user edit(change app) on buttons dynamically. bBrowser & bCalculator are just the labels, lets just say there are 2 buttons b1 & b2. – user2551070 Jul 05 '13 at 19:12
  • And now if the user holds b1 it should opens the app list (MAIN_ACTIVITY) and when it selects an app it should kind of attach that particular app on the b1. And same thing happens for the button b2 also. I hope you understand what i'm trying to say here. Sorry the comment was long had to add 2 comments. Thanks again! – user2551070 Jul 05 '13 at 19:14
  • finally the button works, now user can assign distinct app on buttons & the buttons can open the respective app. I couldn't have done this without you JONG. I mean seariously buddy, thanks a ton. Many many many......thanks to you!:) Here is what I did... – user2551070 Jul 06 '13 at 06:30
  • Great :) Please accept the answer to mark the question as solved. Read about it here: http://stackoverflow.com/help/accepted-answer – Jong Jul 06 '13 at 09:37
  • oh sorry about not accepting your answer, I didn't know about that. This was actually my first post. Now I've accepted your answer. Thanks again Jong. – user2551070 Jul 06 '13 at 13:10
  • hey Jong I got a little problem here, I can get package name & app label that's fine but how can I get the icon when an app is selected in main_activity & returns the result(package icon) to test_activity via onActivityResult.? – user2551070 Jul 07 '13 at 08:51
  • You can use the `ApplicationInfo.icon` field to get the ID of the app icon in its resources, and get it from that apps resources. To get the resources of another application, look here: http://stackoverflow.com/questions/7205415/getting-resources-of-another-application – Jong Jul 07 '13 at 15:47
  • tried everything but didn't get success. Would you care to write few lines of code, I'm in the middle of developing something please... – user2551070 Jul 08 '13 at 05:40
  • I will. But this is falling off the question's topic. Generally you would post a new question if you need more help. – Jong Jul 08 '13 at 09:29
  • I've already posted the question but I got, not even single answer that's why I'm asking here...here check the link http://stackoverflow.com/questions/17511671/how-to-get-the-icon-from-packagemanager-pass-the-icon-to-other-activity – user2551070 Jul 08 '13 at 13:02