daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Aug 12th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.lgfischer.widgethost;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import android.app.Activity;
  7. import android.appwidget.AppWidgetHost;
  8. import android.appwidget.AppWidgetHostView;
  9. import android.appwidget.AppWidgetManager;
  10. import android.appwidget.AppWidgetProviderInfo;
  11. import android.content.ComponentName;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.Menu;
  15. import android.view.MenuInflater;
  16. import android.view.MenuItem;
  17. import android.view.ViewGroup;
  18.  
  19. public class MYWidgetHostActivity extends Activity {
  20.  
  21. static final String TAG = "MYWidgetHostActivity";
  22.  
  23. AppWidgetManager mAppWidgetManager;
  24. AppWidgetHost mAppWidgetHost;
  25.  
  26. ViewGroup mainlayout;
  27.  
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30.     super.onCreate(savedInstanceState);
  31.     setContentView(R.layout.main);
  32.  
  33.     mainlayout = (ViewGroup) findViewById(R.id.main_layout);
  34.  
  35.     mAppWidgetManager = AppWidgetManager.getInstance(this);
  36.     mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);
  37. }
  38.  
  39. @Override
  40. protected void onStart() {
  41.     super.onStart();
  42.     mAppWidgetHost.startListening();
  43. }
  44.  
  45. /**
  46.  * Stop listen for updates for our widgets (saving battery).
  47.  */
  48. @Override
  49. protected void onStop() {
  50.     super.onStop();
  51.     mAppWidgetHost.stopListening();
  52. }
  53.  
  54. public void my() {
  55.     mAppWidgetManager = AppWidgetManager.getInstance(this);
  56.     AppWidgetProviderInfo newAppWidgetProviderInfo = new AppWidgetProviderInfo();
  57.     ComponentName cn = new ComponentName("com.dgflip.scada",
  58.             "com.dgflip.service.AppWidget");
  59.     // Get an id
  60.     int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
  61.  
  62.     // Get the list of installed widgets
  63.     List<AppWidgetProviderInfo> appWidgetInfos = new ArrayList<AppWidgetProviderInfo>();
  64.     appWidgetInfos = mAppWidgetManager.getInstalledProviders();
  65.  
  66.     for (int j = 0; j < appWidgetInfos.size(); j++) {
  67.         if (appWidgetInfos.get(j).provider.getPackageName().equals(
  68.                 "com.digiflip.scada")
  69.                 && appWidgetInfos.get(j).provider.getClassName().equals(
  70.                         "com.digiflip.service.AppWidget")) {
  71.             // Get the full info of the required widget
  72.             newAppWidgetProviderInfo = appWidgetInfos.get(j);
  73.             break;
  74.         }
  75.     }
  76.     mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
  77.     AppWidgetHostView hostView = mAppWidgetHost.createView(this,
  78.             appWidgetId, newAppWidgetProviderInfo);
  79.     hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
  80.     mainlayout.addView(hostView);
  81. }
  82.  
  83. /**
  84.  * Handles the menu.
  85.  */
  86. @Override
  87. public boolean onOptionsItemSelected(MenuItem item) {
  88.     Log.i(TAG,
  89.             "Menu selected: " + item.getTitle() + " / " + item.getItemId()
  90.                     + " / " + R.id.addWidget);
  91.     switch (item.getItemId()) {
  92.     case R.id.addWidget:
  93.         my();
  94.         return true;
  95.     }
  96.     return super.onOptionsItemSelected(item);
  97. }
  98.  
  99. /**
  100.  * Creates the menu with options to add and remove widgets.
  101.  */
  102. @Override
  103. public boolean onCreateOptionsMenu(Menu menu) {
  104.     MenuInflater inflater = getMenuInflater();
  105.     inflater.inflate(R.menu.widget_menu, menu);
  106.     return true;
  107. }
  108.  }
  109.    
  110. AppWidgetManager mAppWidgetManager =
  111.     context.getSystemService(AppWidgetManager.class);
  112.  
  113. AppWidgetProviderInfo myWidgetProviderInfo = new AppWidgetProviderInfo();
  114. ComponentName myProvider = myWidgetProviderInfo.provider;
  115.  
  116. if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
  117. // Create the PendingIntent object only if your app needs to be notified
  118. // that the user allowed the widget to be pinned. Note that, if the pinning
  119. // operation fails, your app isn't notified.
  120. Intent pinnedWidgetCallbackIntent = new Intent( ... );
  121.  
  122. // Configure the intent so that your app's broadcast receiver gets
  123. // the callback successfully. This callback receives the ID of the
  124. // newly-pinned widget (EXTRA_APPWIDGET_ID).
  125. PendingIntent successCallback = PendingIntent.createBroadcast(context, 0,
  126.         pinnedWidgetCallbackIntent);
  127.  
  128. mAppWidgetManager.requestPinAppWidget(myProvider, null,
  129.         successCallback.getIntentSender());
RAW Paste Data
Top