0

我是新的Android和我有一點問題,我已經搜查多個論壇並沒有什麼..Android小工具失去未決意圖

我有一個簡單的小工具,顯示電池並點擊打開彈出式主題的活動。

所以我這個文件太多了,可能有些方法錯了?

WidgetProvider:

public class Widget extends AppWidgetProvider { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     super.onReceive(context, intent); 
     context.startService(new Intent(context, Service.class)); 
    } 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
      int[] appWidgetIds) { 
     // TODO Auto-generated method stub 
     ComponentName thisWidget = new ComponentName(context, 
       Widget.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(context); 

     int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 
     for (int widgetId : allWidgetIds) { 

      RemoteViews updateViews = new RemoteViews(context.getPackageName(), 
        R.layout.widget_layout); 

      Intent intent = new Intent(context, Popup.class); 
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
        intent, 0); 
      updateViews.setOnClickPendingIntent(R.id.widget_layout, 
        pendingIntent); 

      manager.updateAppWidget(widgetId, updateViews); 

     } 
    } 

    @Override 
    public void onDisabled(Context context) { 
     // TODO Auto-generated method stub 
     context.stopService(new Intent(context, Service.class)); 
     super.onDisabled(context); 
    } 
} 

而且我的服務:

public class Service extends Service { 

    private int batteryLevel = 0; 
    RemoteViews updateViews; 

    private BroadcastReceiver receiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 
       batteryLevel = intent.getIntExtra("level", 0); 
       updateViews = new RemoteViews(context.getPackageName(), 
         R.layout.widget_layout); 

       updateViews.setImageViewBitmap(R.id.imgBattery, 
       buildBitmap(batteryLevel + "%", context)); 

       ComponentName thisWidget = new ComponentName(context, 
         Widget.class); 
       AppWidgetManager manager = AppWidgetManager 
         .getInstance(context); 
       manager.updateAppWidget(thisWidget, updateViews); 
      } 
     } 
    }; 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     IntentFilter intentFilter = new IntentFilter(); 
     intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 
     registerReceiver(receiver, intentFilter); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     unregisterReceiver(Receiver); 
     super.onDestroy(); 
    } 
} 

有人可以幫我嗎?而除此之外,該服務具有30MB ...同時運行

回答

2

讓我們這容易讓你:

private BroadcastReceiver receiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 
      batteryLevel = intent.getIntExtra("level", 0); 
      updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 

      updateViews.setImageViewBitmap(R.id.imgBattery, buildBitmap(batteryLevel + "%", context)); 

      Intent intent = new Intent(context, Popup.class); 
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
      updateViews.setOnClickPendingIntent(R.id.widget_layout, pendingIntent); 

      ComponentName thisWidget = new ComponentName(context, Widget.class); 
      AppWidgetManager manager = AppWidgetManager.getInstance(context); 
      manager.updateAppWidget(thisWidget, updateViews); 
     } 
    } 
}; 
+0

謝謝! 1.我在onUpdate(Widget類)上刪除了什麼? 2. onEnabled我已經通過啓動服務,沒關係?因爲當我重新啓動我的手機它只更新電池更換.. – FilipeOS 2013-04-30 21:42:57

+0

1.對不起,我不明白你的第一個問題 2. onEnabled()僅在App Widget第一次創建實例時調用。例如,如果用戶添加了App Widget的兩個實例,這只是第一次。這就是爲什麼服務在重新啓動後沒有啓動的原因。從onUpdate()或onReceive()啓動服務,但要確保它在應用程序的生命週期中只發生一次。使用例如一個靜態布爾變量來檢查它是否已經啓動。 – 2013-05-01 01:45:35

+0

1.我必須刪除我的窗口小部件類中的一些內容,因爲我已經制定了服務意圖?刪除重複的代碼? 2.我用這個例子http://stackoverflow.com/questions/600207/android-check-if-a-service-is-running哪裏沒有運行然後啓動它,好主意?更好的改善?謝謝! – FilipeOS 2013-05-02 08:57:29