我有三个classess.activity,service和appwidget。我在appwidget中有两个按钮。 并且应该更新该按钮小部件,同时应该调用我的活动中的方法,在我的活动中有一些按钮,当我点击我的活动小部件中的按钮时应该更新。
在此部分代码未使用。
这是我的代码。
public class MediaAppWidgetProvider extends AppWidgetProvider {
//update rate in milliseconds
public static final int UPDATE_RATE = 1000;
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
setAlarm(context, appWidgetId, -1);
}
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
context.stopService(new Intent(context,BackService.class));
super.onDisabled(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
setAlarm(context, appWidgetId, UPDATE_RATE);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public static void setAlarm(Context context, int appWidgetId, int updateRate) {
PendingIntent newPending = makeControlPendingIntent(context,BackService.UPDATE,appWidgetId);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (updateRate >= 0) {
alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRate, newPending);
} else {
// on a negative updateRate stop the refreshing
alarms.cancel(newPending);
}
}
public static PendingIntent makeControlPendingIntent(Context context, String command, int appWidgetId) {
Intent active = new Intent(context,BackService.class);
active.setAction(command);
active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
//this Uri data is to make the PendingIntent unique, so it wont be updated by FLAG_UPDATE_CURRENT
//so if there are multiple widget instances they wont override each other
Uri data = Uri.withAppendedPath(Uri.parse("MediaAppWidgetProvider://widget/id/#"+command+appWidgetId), String.valueOf(appWidgetId));
active.setData(data);
return(PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
public class BackService extends Service{
public static final String UPDATE = "update";
public static final String PLUS = "plus";
public static final String MINUS = "minus";
public static final long MODIFY= 86400000;
@Override
public void onStart(Intent intent, int startId) {
String command = intent.getAction();
int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID);
RemoteViews remoteView = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.album_appwidget);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(getApplicationContext());
SharedPreferences prefs = getApplicationContext().getSharedPreferences(
"prefs", 0);
//plus button pressed
if(command.equals(PLUS)){
SharedPreferences.Editor edit=prefs.edit();
edit.putLong("goal" + appWidgetId,prefs.getLong("goal" + appWidgetId, 0)+MODIFY);
edit.commit();
//minus button pressed
}else if(command.equals(MINUS)){
SharedPreferences.Editor edit=prefs.edit();
edit.putLong("goal" + appWidgetId,prefs.getLong("goal" + appWidgetId, 0)-MODIFY);
edit.commit();
}
long goal = prefs.getLong("goal" + appWidgetId, 0);
//compute the time left
long left = goal - new Date().getTime();
int days = (int) Math.floor(left / (long) (60 * 60 * 24 * 1000));
left = left - days * (long) (60 * 60 * 24 * 1000);
int hours = (int) Math.floor(left / (60 * 60 * 1000));
left = left - hours * (long) (60 * 60 * 1000);
int mins = (int) Math.floor(left / (60 * 1000));
left = left - mins * (long) (60 * 1000);
int secs = (int) Math.floor(left / (1000));
//put the text into the textView
remoteView.setTextViewText(R.id.title, days + " days\n" + hours
+ " hours " + mins + " mins " + secs + " secs left");
//set buttons
remoteView.setOnClickPendingIntent(R.id.control_play,MediaAppWidgetProvider.makeControlPendingIntent(getApplicationContext(),PLUS,appWidgetId));
remoteView.setOnClickPendingIntent(R.id.control_next,MediaAppWidgetProvider.makeControlPendingIntent(getApplicationContext(),MINUS,appWidgetId));
// apply changes to widget
appWidgetManager.updateAppWidget(appWidgetId, remoteView);
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
这是我的oncreate方法代码。
Intent launchIntent = getIntent();
Bundle extras = launchIntent.getExtras();
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
// set the result for cancel first
Intent cancelResultValue = new Intent();
cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
setResult(RESULT_CANCELED, cancelResultValue);
SharedPreferences prefs = self.getSharedPreferences("prefs", 0);
SharedPreferences.Editor edit = prefs.edit();
edit.putLong("goal" + appWidgetId, 0l);
edit.commit();
// fire an update to display initial state of the widget
PendingIntent updatepending = MediaAppWidgetProvider
.makeControlPendingIntent(self,
BackService.UPDATE, appWidgetId);
try {
updatepending.send();
} catch (CanceledException e) {
e.printStackTrace();
}
// change the result to OK
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
setResult(RESULT_OK, resultValue);
我的问题: 1.为什么我收到此错误“无法启动服务意图” 2.i只需要在单击我的活动按钮时更新小部件,当我点击小部件按钮时,应该更新小部件,并且应该从服务类调用方法。
这是我的日志。
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger( 31): write blocked for 91 msecs
W/AudioFlinger( 31): write blocked for 66 msecs
W/AudioFlinger( 31): write blocked for 95 msecs
W/AudioFlinger( 31): write blocked for 67 msecs
D/PlayerDriver( 31): buffering (96)
W/AudioFlinger( 31): write blocked for 90 msecs
W/AudioFlinger( 31): write blocked for 69 msecs
W/AudioFlinger( 31): write blocked for 101 msecs
W/AudioFlinger( 31): write blocked for 79 msecs
W/AudioFlinger( 31): write blocked for 82 msecs
W/ActivityManager( 52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger( 31): write blocked for 80 msecs
W/AudioFlinger( 31): write blocked for 66 msecs
W/AudioFlinger( 31): write blocked for 89 msecs
W/AudioFlinger( 31): write blocked for 67 msecs
W/AudioFlinger( 31): write blocked for 91 msecs
W/AudioFlinger( 31): write blocked for 68 msecs
W/AudioFlinger( 31): write blocked for 87 msecs
W/AudioFlinger( 31): write blocked for 70 msecs
D/PlayerDriver( 31): buffering (97)
W/AudioFlinger( 31): write blocked for 65 msecs
W/ActivityManager( 52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger( 31): write blocked for 97 msecs
W/AudioFlinger( 31): write blocked for 70 msecs
W/AudioFlinger( 31): write blocked for 88 msecs
W/AudioFlinger( 31): write blocked for 68 msecs
W/AudioFlinger( 31): write blocked for 79 msecs
W/AudioFlinger( 31): write blocked for 79 msecs
W/AudioFlinger( 31): write blocked for 72 msecs
W/ActivityManager( 52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger( 31): write blocked for 132 msecs
W/AudioFlinger( 31): write blocked for 89 msecs
W/AudioFlinger( 31): write blocked for 72 msecs
W/AudioFlinger( 31): write blocked for 77 msecs
D/PlayerDriver( 31): buffering (97)
W/AudioFlinger( 31): write blocked for 80 msecs
W/AudioFlinger( 31): write blocked for 87 msecs
W/AudioFlinger( 31): write blocked for 68 msecs
W/AudioFlinger( 31): write blocked for 92 msecs
W/AudioFlinger( 31): write blocked for 69 msecs
W/ActivityManager( 52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger( 31): write blocked for 93 msecs
W/AudioFlinger( 31): write blocked for 66 msecs
W/AudioFlinger( 31): write blocked for 86 msecs
W/AudioFlinger( 31): write blocked for 73 msecs
W/AudioFlinger( 31): write blocked for 72 msecs
W/AudioFlinger( 31): write blocked for 88 msecs
W/AudioFlinger( 31): write blocked for 68 msecs
W/AudioFlinger( 31): write blocked for 96 msecs
W/AudioFlinger( 31): write blocked for 69 msecs
W/ActivityManager( 52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger( 31): write blocked for 103 msecs
W/AudioFlinger( 31): write blocked for 83 msecs
W/AudioFlinger( 31): write blocked for 67 msecs
D/PlayerDriver( 31): buffering (97)
W/AudioFlinger( 31): write blocked for 90 msecs
W/AudioFlinger( 31): write blocked for 80 msecs
D/PlayerDriver( 31): buffering (98)
W/AudioFlinger( 31): write blocked for 72 msecs
W/AudioFlinger( 31): write blocked for 66 msecs
W/AudioFlinger( 31): write blocked for 90 msecs
W/ActivityManager( 52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger( 31): write blocked for 93 msecs
W/AudioFlinger( 31): write blocked for 91 msecs
W/AudioFlinger( 31): write blocked for 68 msecs
W/AudioFlinger( 31): write blocked for 66 msecs
W/AudioFlinger( 31): write blocked for 96 msecs
W/AudioFlinger( 31): write blocked for 88 msecs
thankx
答案 0 :(得分:0)
查看http://programmerbruce.blogspot.com/2011/04/simple-complete-app-widget-part-1.html以获取App Widget的完整工作示例,使用Intent成功启动活动和App Widget更新。
如果您仍然遇到困难,那么如果您要发布完整最少的代码(包括所有XML)来帮助您,那么它可以帮助您帮助您复制您的问题。