首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android小部件启动应用程序和检测应用程序未找到

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

Stack Overflow用户
提问于 2015-08-08 21:07:33
回答 2查看 1.3K关注 0票数 1

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

在我的MainActivity扩展AppWidgetProvider (onUpdate)中,我使用这段代码创建一个挂起的意图,如果安装了该应用程序,它的工作效果很好。

代码语言:javascript
复制
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.whatsapp", "com.whatsapp.Main");
intent.setComponent(cn);

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
views.setOnClickPendingIntent(R.id.imageView, pending);

appWidgetManager.updateAppWidget(currentWidgetId, views);

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

代码语言:javascript
复制
 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();
  }

我的问题是:这段代码检测应用程序是否未安装并显示Toast消息,但只在小部件第一次定位在家中时才显示。如果没有安装App,我需要在触摸图像视图时显示消息。请帮帮忙!

卡拉库里我试着采纳你的建议。这是我在主要活动中的代码,用于将意图附加到imageView:

代码语言:javascript
复制
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);

    }      
  }   
}

这是一个带有BroadcastReceiver的新类(我把简单的代码放在里面,以检查应用程序是否已经安装,只是为了测试):

代码语言:javascript
复制
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();
        }
    }             
  }
}

这是我的宣言:

代码语言:javascript
复制
 <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>

非常感谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-17 00:10:47

我找到了另一种解决办法。工作打招呼,但有一个恼人的小问题。在我的主要活动中,我向imageView附加了一个意图,在这个类中启动一个新的类“控件”,我检查应用程序是否已经安装并启动她,或者发送消息并提供一个下载按钮。问题,is...if,应用程序安装,控制类,启动她,但第二次出现活动(控制)屏幕之前,应用程序打开。很烦人。有人能帮我做最后的修正吗?谢谢。

在onUpdate (主要活动)中:

代码语言:javascript
复制
    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); 

我的新班级(控制室):

代码语言:javascript
复制
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();
                }
            });

        }
    }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2015-08-08 23:05:50

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

编辑

做一个新的BroadcastReceiver。

代码语言:javascript
复制
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中。确保意图筛选器操作与上面的操作匹配。

代码语言:javascript
复制
<receiver android:name=".LaunchExternalAppReceiver">
    <intent-filter>
        <action android:name="some-unique-string-here">
    </intent-filter>
</receiver>

让你的ImageView用这个动作发送广播,而不是试图启动外部应用程序。

代码语言:javascript
复制
Intent intent = new Intent(LaunchExternalAppReceiver.ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.imageView, pendingIntent);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31898598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档