我需要一個簡單的家庭小部件來啓動外部應用程序(例如:whatsapp)。Android小部件啓動應用程序並檢測應用程序未找到
在我的MainActivity中擴展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)第I使用此代碼爲檢測是否安裝了應用程序
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();
}
我的問題是: 如果未安裝App這個代碼檢測並顯示吐司消息但只有小部件第一次定位在家中。 如果應用程序未安裝,我需要在任何時候觸摸圖像視圖時顯示消息。 請幫忙!
Karakuri我嘗試應用您的建議。 這是我的意圖連接到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);
}
}
}
這與廣播接收器的新類(我把我的simpele代碼中,以檢查是否安裝了應用程序,只是爲了測試) :
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>
非常感謝你的幫助。
謝謝Karakury。我嘗試在我的onUpdate部分.... if(PendingIntent.getBroadcast(context,0,intent,0)!= null){要做的事情}但是當widget未打開時仍然出現Toast消息。你能解釋更多嗎? – Daniel
使imageView發送廣播意圖,而不是啓動其他應用程序的意圖。 – Karakuri
這是我的代碼知識。你能告訴我這樣做的代碼嗎?我的代碼將意圖附加到imageView是在這篇文章的頂部。 – Daniel