0

지난 한 주 동안 appwidget에서 작업 해 왔으며 모든 것을 올바르게 수행했다고 생각합니다. 그러나 어떤 이유로 든 업데이트 서비스가 제대로 작동하지 않는 것 같습니다.내 위젯 서비스에 문제가 발생했습니다.

내 코드를지나 가게 해줘. 미안해. 그러나 이것이 위젯을 설치하고 실행하는 데 필요한 것입니다.

MyWidgetProvider :

public class MyWidgetProvider extends AppWidgetProvider { 

private static final String LOG = "$AppWidgetProvider"; 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

    Log.i(LOG, "onUpdate method called"); 

    // Get all ids 
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class); 
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 

    // Build the intent to call the service 
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class); 
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds); 

    // Update the widgets via the service 
    context.startService(intent); 
} 
} 

내 XML 제공

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialLayout="@layout/widget_layout" 
    android:minHeight="200dp" 
    android:minWidth="144dp" 
    android:widgetCategory="home_screen" 
    android:configure="widget.AppWidgetConfigureActivity" 
    android:updatePeriodMillis="5000" > 
</appwidget-provider> 

매니페스트 파일 :

<!-- Widget --> 
    <receiver 
     android:name="widget.MyWidgetProvider" 
     android:icon="@drawable/fsk" 
     android:label="FSK Widget" > 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/app_widget_provider_info" /> 
    </receiver> 
    <!-- Services --> 
    <service android:name=".widget.UpdateWidgetService" > 
    </service> 

내 AppWidgetConfiguration 등급 :

public class AppWidgetConfigureActivity extends Activity { 

public String TAG = "AppWidgetConfigureActivity"; 
private View emptyView; 
private View noDevices; 
private int appWidgetId; 
public static MyDevicesSpinnerAdapter myDeviceAdapter; 
private Spinner myDevicesSpinner; 
private Spinner updateFreqSpinner; 
private Button configureButton; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if (!isLoggedIn()) { 
     Intent intent = new Intent(AppWidgetConfigureActivity.this, LoginActivity.class); 
     intent.putExtra("Source", "widgetConfig"); 
     startActivity(intent); 
    } 
    getWidgetId(); 
    setTitle(R.string.title_activity_app_widget_configure); 
    setContentView(R.layout.activity_app_widget_configure); 

    Intent cancelResultValue = new Intent(); 
    cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
    setResult(RESULT_CANCELED, cancelResultValue); 
    myDevicesSpinner = (Spinner) findViewById(R.id.widget_configure_devices_spinner); 
    updateFreqSpinner = (Spinner) findViewById(R.id.widget_configure_updateFreq_spinner); 
    myDeviceAdapter = new MyDevicesSpinnerAdapter(AppWidgetConfigureActivity.this); 
    myDevicesSpinner.setAdapter(myDeviceAdapter); 

    getMyDevices(); 
    populateFreqSpinner(); 
    configureButton = (Button) findViewById(R.id.widget_configure_configure_button); 
    iAgreeCheckBox = (CheckBox) findViewById(R.id.widget_configure_iAgree_checkbox); 

} 

    private void getWidgetId() { 
    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
    } 
} 


public void onConfigureClicked(View view) { 

    SaveWidgetConfiguration(); 

    // Update the View 
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(AppWidgetConfigureActivity.this); 
    RemoteViews views = new RemoteViews(AppWidgetConfigureActivity.this.getPackageName(), R.layout.widget_layout); 
    appWidgetManager.updateAppWidget(appWidgetId, views); 

    // End the config 
    Intent resultValue = new Intent(); 
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
    setResult(RESULT_OK, resultValue); 
    finish(); 
} 

/** 
* Saves the configs of the widget 
*/ 
private void SaveWidgetConfiguration() { 

    int deviceTypeId = 0; 
    int deviceId = 0; 
    String hashedPasscode = ""; 
    int updateFreq = 30000; 

    SharedPreferences prefs = AppWidgetConfigureActivity.this.getSharedPreferences("prefs", 0); 
    SharedPreferences.Editor edit = prefs.edit(); 
    edit.putInt("Widget_DeviceTypeId:" + appWidgetId, deviceTypeId); 
    edit.putInt("Widget_DeviceId:" + appWidgetId, deviceId); 
    edit.putString("Widget_Passcode:" + appWidgetId, hashedPasscode); 
    edit.putInt("Widget_UpdateFreq:" + appWidgetId, updateFreq); 
    edit.commit(); 
} 
} 
,536,913,632 10

내 UpdateWidget 서비스 :

public class UpdateWidgetService extends Service { 

private static final String LOG = "$widgetUpdateService"; 

@Override 
public void onStart(Intent intent, int startId) { 

    Log.i(LOG, "Called and started"); 

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext()); 
    int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); 

    ComponentName thisWidget = new ComponentName(getApplicationContext(), MyWidgetProvider.class); 
    int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget); 
    Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length)); 
    Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length)); 

    for (int appWidgetId : allWidgetIds) { 
     // create some random data 
     RemoteViews remoteView = getView(this, appWidgetId, allWidgetIds); 
     appWidgetManager.updateAppWidget(appWidgetId, remoteView); 
    } 
    stopSelf(); 
    super.onStart(intent, startId); 
} 

private RemoteViews getView(Context context, int appWidgetId, int[] allWidgetIds) { 
    WidgetPrefs prefs = getWidgetConfiguration(context, appWidgetId); 
    switch (prefs.deviceTypeId) { 
    case 8: { 
     int number = (new Random().nextInt(100)); 

     RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout); 
     Log.w("WidgetExample", String.valueOf(number)); 
     // Set the text 
     remoteViews.setTextViewText(R.id.widget_textview_gpscoords, "Random: " + String.valueOf(number)); 
     // Register an onClickListener 
     Intent clickIntent = new Intent(this.getApplicationContext(), MyWidgetProvider.class); 
     clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
     clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     remoteViews.setOnClickPendingIntent(R.id.widget_button_dissarm, pendingIntent); 
    } 
     break; 
    case 10: { 
    } 
     break; 
    default: { 
     // Error 
    } 
    } 

    return null; 
} 

/** 
* Gets the preferences of the widget 
*/ 
private WidgetPrefs getWidgetConfiguration(Context context, int appWidgetId) { 

    WidgetPrefs wPrefs = new WidgetPrefs(); 
    SharedPreferences prefs = context.getSharedPreferences("prefs", 0); 
    wPrefs.deviceTypeId = prefs.getInt("Widget_DeviceTypeId:" + appWidgetId, wPrefs.deviceTypeId); 
    wPrefs.deviceId = prefs.getInt("Widget_DeviceId:" + appWidgetId, wPrefs.deviceId); 
    wPrefs.hashedPasscode = prefs.getString("Widget_Passcode:" + appWidgetId, wPrefs.hashedPasscode); 
    wPrefs.updateFreq = prefs.getInt("Widget_UpdateFreq:" + appWidgetId, wPrefs.updateFreq); 
    return wPrefs; 
} 

/** 
* Holder class to store widget preferences 
* 
*/ 
private class WidgetPrefs { 
    public int deviceTypeId = -1; 
    public int deviceId = -1; 
    public String hashedPasscode = ""; 
    public int updateFreq = 30000; 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

}

지금은 데 문제는, 내 서비스 내 모든 브레이크 포인트 또는 로그 히트를 결코 또는 내 로그 캣,에 나타나지 않는다는 것입니다 내가 설정 한 설정 활동을 제공하는 위젯을 추가하고 위젯을 표시합니다. 하지만 클릭하면됩니다. 나는 내 봉사에서 결코 어떤 활동도하지 않습니까?

무엇이 누락 되었습니까?

답변

1

UpdateWidgetService.getView() 방법에는 2 가지 문제가있는 것 같습니다.


1)

다음 코드를 위젯에 클릭 리스너를 등록 : 위젯을 클릭하면

// Register an onClickListener 
Intent clickIntent = new Intent(this.getApplicationContext(), MyWidgetProvider.class); 
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds); 

, 그것은 당신의 MyWidgetProvider 클래스에 대한 의도를 방송합니다 -이 활동이 아닙니다.

실행하려는 활동의 이름으로 바꾸십시오.


2) 단지 그 코드 아래

,이처럼 PendingIntent을 설정 :

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, 
    clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

문제는 당신이 PendingIntent.getBroadcast()를 사용하여 일반 방송을 전송하는 것입니다.

대신 PendingIntent.getActivity()을 사용하여 활동을 시작하도록 설정해야합니다. 이런 식으로 뭔가 :

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
    clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

은 물론, 그렇지 않은 경우는 실행되지 않습니다, 당신의 활동이 매니페스트 파일에 등록되어 있는지 확인합니다.


나는 당신의 MyWidgetProvider.onUpdate() 방법은 같은 시간에 모든 위젯을 업데이트하려고 시도 것을 볼 그런데

, 대신 그 자체로 하나 하나를 업데이트. 난 당신과 같은 코드를 사용하는 비슷한 질문에 대답을, 당신은 찾을 수 있습니다 그것은 흥미로운 :

  • In Android, how to distinguish between multiple instances of the same widget?
  • 그리고 다른 답변에서, 나는 아주 간단한 위젯 데모에 대한 링크를 제공

    관련 문제