2015-08-08 113 views
0

我需要一個簡單的家庭小部件來啓動外部應用程序(例如: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> 

非常感謝你的幫助。

回答

0

我找到了一種替代解決方案。打招呼,但有一個惱人的小問題。 我在我的主要活動中附加了imageView的意圖,以在此類中啓動一個新類「控件」,我檢查該應用程序是否已安裝並啓動她或發送消息並提供下載按鈕。 問題是...如果安裝了應用程序,控制類將啓動她,但在應用程序打開前第一秒會顯示活動(控制)屏幕。很煩人。 有人可以幫助我做出這個最終修復?謝謝。

在的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(); 
       } 
      }); 

     } 
    } 
    } 
} 
0

讓ImageView單擊發送廣播到您的應用程序中的接收器(使用PendingIntent.getBroadcast())。在該接收器中,您可以檢查外部應用程序是否已安裝並從此處啓動。

編輯

建立一個新的廣播接收器。

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。確保意圖過濾器操作符合上面的操作。

<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); 
+0

謝謝Karakury。我嘗試在我的onUpdate部分.... if(PendingIntent.getBroadcast(context,0,intent,0)!= null){要做的事情}但是當widget未打開時仍然出現Toast消息。你能解釋更多嗎? – Daniel

+0

使imageView發送廣播意圖,而不是啓動其他應用程序的意圖。 – Karakuri

+0

這是我的代碼知識。你能告訴我這樣做的代碼嗎?我的代碼將意圖附加到imageView是在這篇文章的頂部。 – Daniel

相關問題