Android小部件启动App和检测应用程序未找到

时间:2015-08-08 21:07:33

标签: android widget launching-application

我需要一个简单的家庭小工具来启动外部应用程序(例如:whatsapp)。

在我的MainActivity扩展AppWidgetProvider(onUpdate)中,我使用此代码创建待处理的意图,并且如果安装了App,则效果很好。

 PackageManager pm = context.getPackageManager();

        List<ApplicationInfo> list = pm.getInstalledApplications(0);

        for (int aa = 0; aa < list.size(); aa++) {         
            if(list.get(aa).packageName.equals("com.whatsapp")){  
                 //app istalled
                noapp = 0;
            }
            else{
                //app not istalled
                noapp1 = 1;         
            }
        }         
    if(noapp1==1){
    Toast.makeText(context, "Whatsapp not installed", Toast.LENGTH_SHORT).show();
  }

在(onReceive)部分,我使用此代码检测是否安装了应用程序

public class MainActivity extends AppWidgetProvider{

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    for(int i=0; i<appWidgetIds.length; i++){
        int currentWidgetId = appWidgetIds[i];

        Intent intent = new Intent(LaunchExternalAppReceiver.ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
        views.setOnClickPendingIntent(R.id.imageView, pendingIntent); 
        // Tell the AppWidgetManager to perform an update on the current App Widget
        appWidgetManager.updateAppWidget(currentWidgetId, views);

    }      
  }   
}

我的问题是: 此代码检测是否未安装App并显示Toast消息,但仅在第一次将小部件放置在主目录中时显示。 如果未安装App,我需要在触摸imageview的任何时候显示消息。 请帮助!

Karakuri我尝试应用你的建议。 这是我在主要活动中的代码,用于将意图附加到imageView:

public class LaunchExternalAppReceiver extends BroadcastReceiver {
public static final String ACTION = "control";

@Override
public void onReceive(Context context, Intent intent) {

    PackageManager pm = context.getPackageManager();

    List<ApplicationInfo> list = pm.getInstalledApplications(0);

    for (int aa = 0; aa < list.size(); aa++) {  

        if(list.get(aa).packageName.equals("com.whatsapp")){  
            //app istalled

        }
        else{
            //app not istalled             
            Toast.makeText(context, "Whatsapp not installed", Toast.LENGTH_SHORT).show();
        }
    }             
  }
}

这是带有BroadcastReceiver的新类(我在我的simpele代码里面检查是否安装了App,仅用于测试):

 <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" /><application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/mywidget" />
    </receiver>
    <receiver android:name=".LaunchExternalAppReceiver" >
        <intent-filter>
            <action android:name="control" >
            </action>
        </intent-filter>
    </receiver>
</application>

这是我的清单:

.PSD

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

让ImageView点击向您的应用中的接收者发送广播(使用PendingIntent.getBroadcast())。在该接收器中,您可以检查是否已安装外部应用程序并从那里启动它。

修改

制作新的BroadcastReceiver。

public class LaunchExternalAppReceiver extends BroadcastRecevier {
    public static final String ACTION = "some-unique-string-here";

    @Override
    public void onReceive(Context context, Intent intent) {
        // check if the external app is installed and launch it
        PackageManager pm = context.getPackageManager();
        try {
            pm.getApplicationInfo("com.whatsapp", 0); // throws if not found
            Intent intent = pm.getLaunchIntentForPackage("com.whatsapp");
            if (intent != null) {
                context.startActivity(intent);
            }
        } catch (NameNotFoundException e) {
            // package not found, you can show the Toast here
        }
    }
}

将其添加到AndroidManifest。确保intent filter操作符合上面的ACTION。

<receiver android:name=".LaunchExternalAppReceiver">
    <intent-filter>
        <action android:name="some-unique-string-here">
    </intent-filter>
</receiver>

让您的ImageView使用此操作发送广播,而不是尝试启动外部应用。

Intent intent = new Intent(LaunchExternalAppReceiver.ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.imageView, pendingIntent);

答案 1 :(得分:0)

我找到了另一种解决方案。工作迎接,但有一个恼人的小问题。 我在我的主要活动中附加了一个意图到imageView,在这个类中启动一个新类“控件”我检查是否安装了应用程序并启动她或发送消息并提供下载按钮。 问题是......如果安装了应用程序,控制类会启动她,但是在App打开之前会出现一秒钟的活动(控制)屏幕。很烦人。 有人可以帮我做最后的修复吗?感谢。

在onUpdate(主要活动)中:

    Intent controlIntent = new Intent(context, Control.class);          
    PendingIntent pendingc1 = PendingIntent.getActivity(context, 0, controlIntent, 0);
    RemoteViews views1 = new RemoteViews(context.getPackageName(), R.layout.activity_main);
    views1.setOnClickPendingIntent(R.id.imageView, pendingc1);
    appWidgetManager.updateAppWidget(currentWidgetId, views1); 

我的新班级(控制):

public class Control extends Activity {
private TextView testo;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PackageManager pm = this.getPackageManager();

    List<ApplicationInfo> list = pm.getInstalledApplications(0);

    for (int aa = 0; aa < list.size(); aa++) {

        if (list.get(aa).packageName.equals("com.whatsapp")) {
            // app istalled

            Intent launchIntent = getPackageManager()
                    .getLaunchIntentForPackage("com.whatsapp");
            startActivity(launchIntent);
            Control.this.finish();

        } else {
            setContentView(R.layout.control);
            // app not istalled
            testo = (TextView) findViewById(R.id.message);
            String text = "WhatsApp is Not Installed\nPlease Download Whatsapp from Play Store";
            testo.setText(text);

            Button button = (Button) findViewById(R.id.exit);
            button.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    Control.this.finish();
                }
            });

            Button button2 = (Button) findViewById(R.id.download);
            button2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri
                            .parse("market://details?id=" + "com.whatsapp")));
                    Control.this.finish();
                }
            });

        }
    }
  }
}