지난 한 주 동안 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;
}
}
지금은 데 문제는, 내 서비스 내 모든 브레이크 포인트 또는 로그 히트를 결코 또는 내 로그 캣,에 나타나지 않는다는 것입니다 내가 설정 한 설정 활동을 제공하는 위젯을 추가하고 위젯을 표시합니다. 하지만 클릭하면됩니다. 나는 내 봉사에서 결코 어떤 활동도하지 않습니까?
무엇이 누락 되었습니까?