2015-05-08 37 views
0

我試圖點擊小部件中的按鈕後啓動服務,但不知何故該服務無法啓動。我看着整個互聯網,並沒有找到答案。按鈕點擊小部件後服務不啓動

這是的onUpdate方法

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

     // Create an intent to launch the service 
     Intent serviceIntent = new Intent(context, MyService.class); 

     // PendingIntent is required for the onClickPendingIntent that actually 
     // starts the service from a button click 
     PendingIntent pendingServiceIntent = 
       PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Get the layout for the App Widget and attach a click listener to the 
     // button 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.meat); 
     appWidgetManager.updateAppWidget(appWidgetIds, views); 
     Log.i(TAG, "onUpdate is on"); 
     views.setOnClickPendingIntent(R.id.button, pendingServiceIntent); 
     context.startService(serviceIntent); 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 

    } 

我還試圖onRecive方法

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent intent1 = new Intent (context, MyService.class); 
    intent1.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startService (intent1); 
    super.onReceive(context, intent); 
} 

我通過吐司消息試圖測試

public class MyService extends Service { 

    Context context = getApplicationContext(); 

    @Override 
    public void onCreate() { 
     Toast.makeText(context, "the service started", Toast.LENGTH_SHORT).show(); 
    } 

這是onStartCommand

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     timer = new MyCounter(21600000,1000); 
     RemoteViews v = new RemoteViews(getPackageName(),R.layout.meat); 
     v.setTextViewText(R.id.textView2, getCurrentTime()); 
     appWidgetManager.updateAppWidget(thisWidget, v); 
     timer.start(); 
     Toast.makeText(context, "the service started", Toast.LENGTH_SHORT).show(); 
     appWidgetManager.updateAppWidget(thisWidget, v); 
     return START_STICKY; 
    } 

我的清單

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <receiver android:name=".meat" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/meat_info" /> 
     </receiver> 

     <service 
      android:name=".MyService" 
      android:enabled="true" 
      android:exported="true" > 
      <intent-filter> 
       <action android:name="ACTION_WIDGET_CLICK_RECEIVER"/> 
      </intent-filter> 
     </service> 
    </application> 

回答

0

要開始,你必須調用的onReceive startService方法的服務。因此,只需更換這行:

context.startActivity (intent1); 

有了這個:

context.startService(intent1); 

而且還一定要重寫onStartCommand方法開始服務

+0

謝謝,但我已經嘗試過了,忽略了最低工作。你有其他想法嗎? – user4811620

+0

@ user4811620您是否嘗試過在onUpdate和onReceive方法中添加斷點,以檢查它們是否被調用?也可以嘗試在服務中添加斷點。爲了說明問題,Toast不顯示的事實並不意味着你的服務沒有啓動,服務也不是意在與UI交互 – Chaosit

+0

我在調試模式下進行了檢查,onUpdate和onReceive未被調用。你有什麼想法,爲什麼? – user4811620

相關問題