Install/run applications from within another application

Put your problem here if it does not fit any of the other categories.

Install/run applications from within another application

Postby calleandersson » Wed Jul 01, 2009 2:06 pm

Hi,

I'm going to create an application (using Android 1.5) which can download and install other applications and also be able to remove these applications when needed. I have tried to do this in two different ways but havn't had any success:

-- A: the PackageManager way --

Using the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. getPackageManager().installPackage(Uri.parse(url));
Parsed in 0.029 seconds, using GeSHi 1.0.8.4

an SecurityException occur since (as I understand) it isn't possible for an application to be granted the INSTALL_PACKAGES permission (which is needed by the installPackage() method) unless the program has system rights.

A1. Is it correct that system rights is needed by an application to be granted INSTALL_PACKAGES permission?

A2. How can an application acquire system rights?

A3. Will this approach generate some kind of platform specific install popups (or something like that) or could an application be installed without any user interaction required?


-- B: the Intent way --

I download an .apk-file using code similar to the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. URL sourceUrl = new URL(source);
  2.  
  3. Object data = sourceUrl.getContent();
  4.  
  5. String fileName = sourceUrl.getFile().substring(fileName.lastIndexOf('/') + 1);
  6.  
  7. // create/open file in the 'data/data/<app namespace>/files' directory
  8.  
  9. FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
  10.  
  11. int read = 0;
  12.  
  13. byte[] buffer = new byte[512];
  14.  
  15. BufferedInputStream bis = new BufferedInputStream((InputStream) data);
  16.  
  17. do{
  18.  
  19.         read = bis.read(buffer);
  20.  
  21.         if(read > 0){
  22.  
  23.                 fos.write(buffer, 0, read);
  24.  
  25.         }
  26.  
  27. }while(read != -1);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

and then i try to invoke an installation of the application by using the following code (which I belive should bring the system UI up for the user to confirm the install):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = new Intent();
  2.  
  3. intent.setAction(android.content.Intent.ACTION_VIEW);
  4.  
  5. intent.setDataAndType(Uri.parse("/data/data/test.calle.helloworld/files/AndroidHelloWorld.apk"), "application/vnd.android.package-archive");
  6.  
  7. startActivity(intent);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

but this only generates an ActivityNotFoundException:
[font=Courier New]07-01 10:11:05.354: ERROR/AndroidRuntime(2480): android.content.ActivityNotFoundException: No Activity found to handle Intent { action=android.intent.action.VIEW data=/data/data/test.calle.helloworld/files/AndroidHelloWorld.apk type=application/vnd.android.package-archive }[/font]

B1. What am I doing wrong/missing in the attempt to install the downloaded application?

B2. Will this approach bring the system UI up (for the user to confirm the install) or have I misunderstood something?

B3. Could I use the same Intent code to start an already installed application or do I need change some input data?

B4. Is it possible to initiate an uninstall of an application with a similar approach (without beeing granted the REMOVE_PACKAGES permission)?

B5. Is there some cleaner/easier way of downloading an entire file from the internet to the file system and should I use 'Context.MODE_PRIVATE' when I call the openFileOutput() method?
calleandersson
Freshman
Freshman
 
Posts: 2
Joined: Wed Jul 01, 2009 1:49 pm

Top

Postby villain_dm » Thu Jul 02, 2009 5:15 pm

this should work:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = new Intent(Intent.ACTION_VIEW);
  2.  
  3. intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
  4.  
  5. startActivity(intent);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


that's because intent filter is:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <activity android:name=".PackageInstallerActivity">
  2.  
  3.             <intent-filter>
  4.  
  5.                 <action android:name="android.intent.action.VIEW" />
  6.  
  7.                 <category android:name="android.intent.category.DEFAULT" />
  8.  
  9.                 <data android:scheme="content" />
  10.  
  11.                 <data android:scheme="file" />
  12.  
  13.                 <data android:mimeType="application/vnd.android.package-archive" />
  14.  
  15.             </intent-filter>
  16.  
  17.         </activity>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


intent filter for uninstallation:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <activity android:name=".UninstallerActivity">
  2.  
  3.             <intent-filter>
  4.  
  5.                 <action android:name="android.intent.action.VIEW" />
  6.  
  7.                 <action android:name="android.intent.action.DELETE" />
  8.  
  9.                 <category android:name="android.intent.category.DEFAULT" />
  10.  
  11.                 <data android:scheme="package" />
  12.  
  13.             </intent-filter>
  14.  
  15.         </activity>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
villain_dm
Freshman
Freshman
 
Posts: 4
Joined: Fri Dec 14, 2007 11:17 pm
Location: St.Petersburg, Russia

Postby calleandersson » Fri Jul 03, 2009 4:02 pm

Hi,

Thanks for your answer.

I managed to invoke the system installation UI using code very similar to the one you suggested.

It seems that the .apk file has to be stored on the SD card since I got an error due to permission issues when trying to invoke the system installation UI when the .apk file was located in the private directory of my application.

Do you know if it is possible to install an application from an internet url or from the private directory of my application? I don't want to save the .apk file on the SD card since I believe it would be possible to use the same SD card in another phone (or copy the .apk file to another SD card used by another phone) and install the application on that phone too. I want to restrict the application file from beeing installed on any other phone except the very same phone which downloaded the .apk file (using my application)?

Do you know if this is possible? (I don't have system rights and therefor can't use the PackageManager.installPackage(Uri) method.)
calleandersson
Freshman
Freshman
 
Posts: 2
Joined: Wed Jul 01, 2009 1:49 pm

Postby songotho » Mon Nov 02, 2009 5:32 pm

Hi,
I have tried to do it, so would you show the simple code to me?
Thanks.
Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Postby poson » Mon Dec 14, 2009 5:01 am

Hi calleandersson:

It may be a late advice for you!
Use villain_dm's way, it can always show a message says "Do you want to install this applications?".So you have time to decide to install or cancel.
In other way,you can make a key register or equel to the phone's information such as phone_id .

Just a advice for you.Also maybe a joke.
Hope can help you !
poson
Freshman
Freshman
 
Posts: 7
Joined: Thu Dec 10, 2009 4:50 am

Reg: install .apk file

Postby Sri » Sun Jan 24, 2010 9:07 am

Hi All,

I implemented the following code for installing .apk file. it's working fine. But It shows up gui and asks like "Do you want to install or not" like this. But i want to disable GUI. i.e installation will do silently in background? is it possible? if yes, Can you please tell the process.

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
Context.startActivity(intent);


I tried PackageManager.installPackage() also. but it says Security Exception.


Thanks,
Sri.
Sri
Developer
Developer
 
Posts: 25
Joined: Fri Dec 04, 2009 2:31 pm

Top

Postby aparna.gopinath » Mon Feb 08, 2010 10:54 am

Hi All,
I want to call an application from within another application .. As In , I have a first application with , say , a button ... So when I click this button , Another application should automatically run . Assuming that the .apk is stored within the sd card .. How can I go about it ??
aparna.gopinath
Freshman
Freshman
 
Posts: 5
Joined: Thu Jul 23, 2009 5:59 am

Re: Reg: install .apk file

Postby Akshata » Fri Apr 30, 2010 7:03 am

Sri wrote:Hi All,

I implemented the following code for installing .apk file. it's working fine. But It shows up gui and asks like "Do you want to install or not" like this. But i want to disable GUI. i.e installation will do silently in background? is it possible? if yes, Can you please tell the process.

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
Context.startActivity(intent);


I tried PackageManager.installPackage() also. but it says Security Exception.


Thanks,
Sri.


I have tried this along with villain_dm's slution.
But for all application it is calling same activity.
When I change the manifest file for unistallation and tried for another application it is giving Package Parse Error.

This is my Code:


Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("file:///data/data/com.android.cab/files/TestApp1.apk")),"application/vnd.android.package-archive");
startActivity(intent);

It gives Parse Error and Warning:
WARN/PackageParser(241): Skipping dir: /file:/data/data/com.android.cab/files/TestApp1.apk
WARN/PackageInstaller(241): Parse error when parsing manifest. Discontinuing installation
Akshata
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Apr 30, 2010 6:36 am

Re: Reg: install .apk file

Postby jonneymendoza » Wed May 05, 2010 9:13 am

Akshata wrote:
Sri wrote:Hi All,

I implemented the following code for installing .apk file. it's working fine. But It shows up gui and asks like "Do you want to install or not" like this. But i want to disable GUI. i.e installation will do silently in background? is it possible? if yes, Can you please tell the process.

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
Context.startActivity(intent);


I tried PackageManager.installPackage() also. but it says Security Exception.


Thanks,
Sri.


I have tried this along with villain_dm's slution.
But for all application it is calling same activity.
When I change the manifest file for unistallation and tried for another application it is giving Package Parse Error.

This is my Code:


Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("file:///data/data/com.android.cab/files/TestApp1.apk")),"application/vnd.android.package-archive");
startActivity(intent);

It gives Parse Error and Warning:
WARN/PackageParser(241): Skipping dir: /file:/data/data/com.android.cab/files/TestApp1.apk
WARN/PackageInstaller(241): Parse error when parsing manifest. Discontinuing installation


Hi. did you manage to fix this issue? im getting a parsing error as well
jonneymendoza
Developer
Developer
 
Posts: 36
Joined: Fri Sep 04, 2009 10:30 am

Re: Reg: install .apk file

Postby Akshata » Wed May 05, 2010 9:22 am

No. Not Yet. If we are using villain_dm's method, bydefault the same activity is called. So that new activity is not called.
We have to uninstall it first and try for another.....

But I have tried another way of doing this but it is not asking for installation.Directly the application is called.

The code is :
Intent i = new Intent();
i.setClassName("<package_name>","<Class Name(with packagae name)>");
startActivity(i);

Try this, this works for me. :)
Akshata
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Apr 30, 2010 6:36 am

Re: Reg: install .apk file

Postby jonneymendoza » Wed May 05, 2010 9:40 am

Akshata wrote:No. Not Yet. If we are using villain_dm's method, bydefault the same activity is called. So that new activity is not called.
We have to uninstall it first and try for another.....

But I have tried another way of doing this but it is not asking for installation.Directly the application is called.

The code is :
Intent i = new Intent();
i.setClassName("<package_name>","<Class Name(with packagae name)>");
startActivity(i);

Try this, this works for me. :)


How are we suppose to know what the class name and package name is? all i have is the apk file path located on the sdcard.

say if the file path is /sdcard/helloWorld.apk How would that look like in the above code?

edit: i used using villain_dm method by including the Intent filters and the parsing error i get is a FileNotFoundException. It cant find the manifest file of the app thats located in the sdcard
jonneymendoza
Developer
Developer
 
Posts: 36
Joined: Fri Sep 04, 2009 10:30 am

Re: Install/run applications from within another application

Postby smodon » Thu Oct 07, 2010 12:56 pm

hey sorry for taking out this old thread but i need this functionality, and i have the same problem as jonney, what do i have tried many things as package_name and class name here but it didnt work...
smodon
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 07, 2010 12:55 pm

Re: Install/run applications from within another application

Postby luutuong » Thu Nov 25, 2010 12:44 pm

@ smodon this my code, i think it helps you

Intent intent = new Intent(Intent.ACTION_RUN);
intent.setComponent(new ComponentName("<packet name>", "<class name>"));
List list = packageManager.queryIntentActivities(intent, packageManager.COMPONENT_ENABLED_STATE_DEFAULT);


if(list.size() > 0)
{
Log.i("Log", "Have application" + list.size());
startActivity(intent);
}
else
{
Log.i("Log", "None application");
}
luutuong
Once Poster
Once Poster
 
Posts: 1
Joined: Fri May 14, 2010 4:03 am

Re: Install/run applications from within another application

Postby saranya » Tue Mar 15, 2011 3:02 pm

Hi All,

I tried the ways that you mentioned above. But while trying to getContent of particular url I got null object.
My code is following

String source = myurl;
String fileName = "";

try{
URL sourceUrl = new URL(source);
Object data = sourceUrl.getContent(); // data object is null
fileName = sourceUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
// create/open file in the 'data/data/<app namespace>/files' directory
Log.v(TAG,"File Name ==> " + fileName);
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
int read = 0;
byte[] buffer = new byte[512];

BufferedInputStream bis = new BufferedInputStream((InputStream)data);
do {
read = bis.read(buffer);
if (read > 0) {
fos.write(buffer, 0, read);
}
} while (read != -1);
Log.v(TAG,"Completed file reading");
}catch(Exception e){
Log.e(TAG,"Exception in install == > " + e.getMessage());
}
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);

I got the null pointer exception while running this. I don't know why it is returning null for content.
help me

Thanks,
Saranya
saranya
Developer
Developer
 
Posts: 48
Joined: Wed Oct 22, 2008 8:06 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot], Yahoo [Bot] and 17 guests