2011-03-07 68 views
2

我有三個classess.activity,服務和appwidget。 appwidget中有兩個按鈕。 和onclicking該按鈕控件應更新爲我的活動以及方法應該被調用,在我的活動還有一些按鈕,當我在我的活動部件點擊按鈕,應該更新。無法啓動服務意圖?

在這部分代碼沒有被使用。

這裏是我的代碼。

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; 
    } 

} 

這裏是我的創建方法代碼。

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.我只是想更新插件在我的活動按鈕被點擊的時候,當我點擊控件按鈕控件應該更新並且應該從服務類中調用方法。

這裏是我的日誌。

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

PLZ加你的日誌... – 2011-03-07 04:50:17

回答