安卓应用程序中有特定应用程序的快捷图标

4
我已经创建了一个安卓应用程序,其中有两个按钮可以从互联网服务器下载apk文件,如果设备上没有安装该应用程序。我想在我的应用程序中提供这些新安装的应用程序的快捷图标。我看到一些关于启动器应用程序的帖子,但它们列出了所有应用程序的快捷方式。我想要特别为我的两个应用程序。有没有办法做到这一点?
这是现有应用程序的代码:
//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>
2个回答

3

我通过保存这些应用程序的ic_launcher并将其用作ImageButtons来实现了一个解决方法,但是当我使用应用程序的packageName时需要一些自动化的东西来生成图标。

// ImageButton的代码:

ImageButton launchApp1=(ImageButton) findViewById(R.id.button3);

    launchApp1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    if(app1Installed){
                        Intent LaunchIntent = getPackageManager()
                                .getLaunchIntentForPackage("com.example.cameraphonegap");
                        startActivity(LaunchIntent);}else{
                             Log.d("installed","App1 not installed on your phone");
                              Toast.makeText(getApplicationContext(),
                              "App1 Not Installed ", Toast.LENGTH_SHORT).show(); }

                }
            });

// activity_main.xml

  <ImageButton
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button1"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="20dp"
        android:background="@null"
        android:contentDescription="@null"
        android:src="@drawable/app1" />

The launch icon for apps which are installed by my applications


1
如何从服务器上提供这些图像呢?您可以在某个地方托管这些应用程序的图像(包名称的键值?),并在应用程序中的列表视图中列出它们。这正是您想要实现的内容,但使用不同的方法来实现它,而不是您正在尝试做的事情。

是的,那正是我想说的。 - Faraz Hassan
我们能否自动化一些操作,而不是实际访问应用程序的ic_launcher?只需使用应用程序的PackageName获取启动快捷方式,这样行吗? - priya3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接