Android 小部件启动应用程序并检测到应用程序未找到
Android widget launch App and detect App Not found
我需要一个简单的主页小部件来启动外部应用程序(例如:whatsapp)。
在我的 MainActivity extends AppWidgetProvider (onUpdate) 中,我使用此代码创建一个挂起的意图,如果安装了该应用程序,它会很好地工作。
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) 部分,我使用此代码检测应用程序是否已安装
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 消息,但仅在小部件第一次位于家中时显示。
如果未安装应用程序,我需要在触摸图像视图时随时显示消息。
请帮忙!
Karakuri 我试着应用你的建议。
这是我在主要 activity 中的代码,用于将意图附加到 imageView:
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 的新 class(我将简单代码放入我的简单代码中以检查应用程序是否已安装,仅用于测试):
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();
}
}
}
}
这是我的清单:
<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>
非常感谢您的帮助。
点击 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 过滤器操作与上面的 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);
我找到了替代解决方案。工作迎接但有一个恼人的小问题。
我在主 activity 的 imageView 上附加了一个意图,以在这个 class 中启动一个新的 class "control" 我检查是否安装了该应用程序并启动她或发送消息并提供下载按钮。
问题是...如果安装了应用程序,控件 class 会启动她,但会在应用程序打开前出现 activity(控件)屏幕一秒钟。很烦人。
有人可以帮助我进行最终修复吗?谢谢
在onUpdate(主要activity)中:
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);
我的新class(对照):
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();
}
});
}
}
}
}
我需要一个简单的主页小部件来启动外部应用程序(例如:whatsapp)。
在我的 MainActivity extends AppWidgetProvider (onUpdate) 中,我使用此代码创建一个挂起的意图,如果安装了该应用程序,它会很好地工作。
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) 部分,我使用此代码检测应用程序是否已安装
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 消息,但仅在小部件第一次位于家中时显示。 如果未安装应用程序,我需要在触摸图像视图时随时显示消息。 请帮忙!
Karakuri 我试着应用你的建议。 这是我在主要 activity 中的代码,用于将意图附加到 imageView:
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 的新 class(我将简单代码放入我的简单代码中以检查应用程序是否已安装,仅用于测试):
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();
}
}
}
}
这是我的清单:
<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>
非常感谢您的帮助。
点击 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 过滤器操作与上面的 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);
我找到了替代解决方案。工作迎接但有一个恼人的小问题。 我在主 activity 的 imageView 上附加了一个意图,以在这个 class 中启动一个新的 class "control" 我检查是否安装了该应用程序并启动她或发送消息并提供下载按钮。 问题是...如果安装了应用程序,控件 class 会启动她,但会在应用程序打开前出现 activity(控件)屏幕一秒钟。很烦人。 有人可以帮助我进行最终修复吗?谢谢
在onUpdate(主要activity)中:
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);
我的新class(对照):
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();
}
});
}
}
}
}