我已经创建了一个安卓应用程序,其中有两个按钮可以从互联网服务器下载apk文件,如果设备上没有安装该应用程序。我想在我的应用程序中提供这些新安装的应用程序的快捷图标。我看到一些关于启动器应用程序的帖子,但它们列出了所有应用程序的快捷方式。我想要特别为我的两个应用程序。有没有办法做到这一点?
这是现有应用程序的代码:
//MainActivity.java
这是现有应用程序的代码:
//MainActivity.java
package com.example.appcommunication;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button app1, app2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
app1 = (Button) findViewById(R.id.button1);
app2 = (Button) findViewById(R.id.button2);
final boolean app1Installed = appInstalledOrNot("com.example.cameraphonegap");
final boolean app2Installed = appInstalledOrNot("com.example.webviewexample");
if(app1Installed){
app1.setEnabled(false);
app1.setText("App1 Installed ");
}else{
app1.setEnabled(true);
}
if(app2Installed){
app2.setEnabled(false);
app2.setText("App2 Installed ");
}else{
app2.setEnabled(true);
}
app1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("App is not installed on your phone");
Log.d("NOT installed","App not installed on your phone");
Toast.makeText(getApplicationContext(), "App1 Not Installed ", Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.dropbox.com/s/9vyvypj9qxjanb/CameraPhoneGap.apk"));
startActivity(browserIntent);
}
});
app2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("App is not installed on your phone");
Log.d("NOT installed","App not installed on your phone");
Toast.makeText(getApplicationContext(), "App2 Not Installed ", Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.dropbox.com/s/r2z299661ibmhm/webviewExample.apk"));
startActivity(browserIntent);
}
});
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity"
android:background="#7FFFD4">
<Button
android:id="@+id/button1"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Install app1"
android:textStyle="bold"
/>
<Button
android:id="@+id/button2"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Install app2"
android:textStyle="bold"
/>
</LinearLayout>
//Android清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appcommunication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.appcommunication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>