2015-11-03 69 views
0

我在Activty中創建了一個按鈕,並通過單擊按鈕我想在android中以編程方式將Appwidget添加到主屏幕。如何通過點擊活動中的按鈕將appwidget添加到主屏幕

實際上,我正在使用appwidgethost類將appwidget添加到主屏幕,但appwidget被添加到活動而不是主屏幕。我的代碼是低於

package com.lgfischer.widgethost; 


import java.util.ArrayList; 
import java.util.List; 
import android.app.Activity; 
import android.appwidget.AppWidgetHost; 
import android.appwidget.AppWidgetHostView; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProviderInfo; 
import android.content.ComponentName; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.ViewGroup; 

public class MYWidgetHostActivity extends Activity { 

static final String TAG = "MYWidgetHostActivity"; 

AppWidgetManager mAppWidgetManager; 
AppWidgetHost mAppWidgetHost; 

ViewGroup mainlayout; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mainlayout = (ViewGroup) findViewById(R.id.main_layout); 

    mAppWidgetManager = AppWidgetManager.getInstance(this); 
    mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    mAppWidgetHost.startListening(); 
} 

/** 
* Stop listen for updates for our widgets (saving battery). 
*/ 
@Override 
protected void onStop() { 
    super.onStop(); 
    mAppWidgetHost.stopListening(); 
} 

public void my() { 
    mAppWidgetManager = AppWidgetManager.getInstance(this); 
    AppWidgetProviderInfo newAppWidgetProviderInfo = new AppWidgetProviderInfo(); 
    ComponentName cn = new ComponentName("com.dgflip.scada", 
      "com.dgflip.service.AppWidget"); 
    // Get an id 
    int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); 

    // Get the list of installed widgets 
    List<AppWidgetProviderInfo> appWidgetInfos = new ArrayList<AppWidgetProviderInfo>(); 
    appWidgetInfos = mAppWidgetManager.getInstalledProviders(); 

    for (int j = 0; j < appWidgetInfos.size(); j++) { 
     if (appWidgetInfos.get(j).provider.getPackageName().equals(
       "com.digiflip.scada") 
       && appWidgetInfos.get(j).provider.getClassName().equals(
         "com.digiflip.service.AppWidget")) { 
      // Get the full info of the required widget 
      newAppWidgetProviderInfo = appWidgetInfos.get(j); 
      break; 
     } 
    } 
    mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn); 
    AppWidgetHostView hostView = mAppWidgetHost.createView(this, 
      appWidgetId, newAppWidgetProviderInfo); 
    hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo); 
    mainlayout.addView(hostView); 
} 

/** 
* Handles the menu. 
*/ 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    Log.i(TAG, 
      "Menu selected: " + item.getTitle() + "/" + item.getItemId() 
        + "/" + R.id.addWidget); 
    switch (item.getItemId()) { 
    case R.id.addWidget: 
     my(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* Creates the menu with options to add and remove widgets. 
*/ 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.widget_menu, menu); 
    return true; 
} 
} 

上面的代碼工作,如果我將appwidget添加到活動。但我想添加appwidget到主屏幕。

其實我不明白如何通過appwidgethostview來查看或引用主屏幕來添加視圖。

請提前幫我關於這個.. thanx。

+0

按照此鏈接,在主屏幕上創建應用程序部件。 https://github.com/ChiragSavsani/FlashLightWidget –

+0

該代碼是否有效? –

+0

你好Chirag thanx求助,但我的任務沒有解決,因爲在這個例子中,appWidget被手動添加到主屏幕,但我想通過編程方式添加appwidget到主屏幕,而不是手動選擇appwidget並放在主屏幕上...... –

回答

0

在Android O中,可以通過編程方式設置應用程序小部件。

AppWidgetManager mAppWidgetManager = 
    context.getSystemService(AppWidgetManager.class); 

AppWidgetProviderInfo myWidgetProviderInfo = new AppWidgetProviderInfo(); 
ComponentName myProvider = myWidgetProviderInfo.provider; 

if (mAppWidgetManager.isRequestPinAppWidgetSupported()) { 
    // Create the PendingIntent object only if your app needs to be notified 
    // that the user allowed the widget to be pinned. Note that, if the pinning 
    // operation fails, your app isn't notified. 
    Intent pinnedWidgetCallbackIntent = new Intent(...); 

    // Configure the intent so that your app's broadcast receiver gets 
    // the callback successfully. This callback receives the ID of the 
    // newly-pinned widget (EXTRA_APPWIDGET_ID). 
    PendingIntent successCallback = PendingIntent.createBroadcast(context, 0, 
      pinnedWidgetCallbackIntent); 

    mAppWidgetManager.requestPinAppWidget(myProvider, null, 
      successCallback.getIntentSender()); 
} 

還檢查了谷歌官方documentation

相關問題