2013-07-07 98 views
5

我想从内部部件启动一个服务,我知道我可以使用的PendingIntent做到像启动/停止服务从窗口小部件

PendingIntent intent = PendingIntent.getService(context, 0, new Intent(context, MyService.class), Intent.FLAG_ACTIVITY_NEW_TASK); 
remoteViews.setOnClickPendingIntent(R.id.buttonWidgetStartService, intent); 

但问题是,我已经使用的PendingIntent上按钮用于其他目的。 于是,我开始尝试使用

context.startService(new Intent(context, MyService.class)); 

,在这里我得到NullPointerException服务。

这里是我的代码:

public class WiNetWidget extends AppWidgetProvider { 

    public static String TOGGLE_WINET = "ToggleWiNetService"; 
    private static boolean serviceRunning = false; 
    private static Intent serviceIntent; 
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     final int N = appWidgetIds.length; 
     serviceIntent = new Intent(context, WiNetService.class); 

     for (int i=0; i<N; i++) { 
      int appWidgetId = appWidgetIds[i]; 
      RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_winet); 

      remoteViews.setViewVisibility(R.id.buttonWidgetLoading, View.INVISIBLE); 
      remoteViews.setViewVisibility(R.id.buttonWidgetStartService, View.VISIBLE); 

      Intent newIntent = new Intent(context, WiNetWidget.class); 
      newIntent.setAction(TOGGLE_WINET); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0); 
      remoteViews.setOnClickPendingIntent(R.id.buttonWidgetStartService, pendingIntent); 
      remoteViews.setOnClickPendingIntent(R.id.buttonWidgetStopService, pendingIntent); 

      appWidgetManager.updateAppWidget(appWidgetId, remoteViews); 
      Log.i(TOGGLE_WINET, "updated"); 
     } 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if(intent.getAction().equals(TOGGLE_WINET)) { 
      RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_winet); 
      if(serviceRunning) { 
       context.stopService(serviceIntent); 
       remoteViews.setViewVisibility(R.id.buttonWidgetStartService, View.VISIBLE); 
       remoteViews.setViewVisibility(R.id.buttonWidgetStopService, View.INVISIBLE); 
       Toast.makeText(context, "serviceStopped", Toast.LENGTH_SHORT).show(); 
      } else { 
       context.startService(serviceIntent); 
       remoteViews.setViewVisibility(R.id.buttonWidgetStopService, View.VISIBLE); 
       remoteViews.setViewVisibility(R.id.buttonWidgetStartService, View.INVISIBLE); 
       Toast.makeText(context, "serviceStarted", Toast.LENGTH_SHORT).show(); 
      } 
      serviceRunning=!serviceRunning; 
      ComponentName componentName = new ComponentName(context, WiNetWidget.class); 
      AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews); 
     } 
     super.onReceive(context, intent); 
    } 
} 

注:如果删除包含startServicestopService线条,一切进展顺利。

在此先感谢! :)

+0

如果你使用'context.startService(new Intent(context,MyService.class));'而不是变量'serviceIntent',它工作吗? –

+0

@Neil Townsend:谢谢你,先生,它的工作。非常感谢!!你能解释一下为什么它不工作?再次感谢。 :) –

+0

请参阅下面的更全面的答案。 –

回答

2

问题是,您创建一次Intent一次,但多次使用它。最好创建一个新的Intent要启动或停止Service如下每次:

@Override 
public void onReceive(Context context, Intent intent) { 
    if(intent.getAction().equals(TOGGLE_WINET)) { 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_winet); 

     // Create a fresh intent 
     Intent serviceIntent = new Intent(context, WiNetService.class); 

     if(serviceRunning) { 
      context.stopService(serviceIntent); 
      remoteViews.setViewVisibility(R.id.buttonWidgetStartService, View.VISIBLE); 
      remoteViews.setViewVisibility(R.id.buttonWidgetStopService, View.INVISIBLE); 
      Toast.makeText(context, "serviceStopped", Toast.LENGTH_SHORT).show(); 
     } else { 
      context.startService(serviceIntent); 
      remoteViews.setViewVisibility(R.id.buttonWidgetStopService, View.VISIBLE); 
      remoteViews.setViewVisibility(R.id.buttonWidgetStartService, View.INVISIBLE); 
      Toast.makeText(context, "serviceStarted", Toast.LENGTH_SHORT).show(); 
     } 
     serviceRunning=!serviceRunning; 
     ComponentName componentName = new ComponentName(context, WiNetWidget.class); 
     AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews); 
    } 
    super.onReceive(context, intent); 
} 
-1
Intent newIntent = new Intent(context, WiNetWidget.class); 
if(newIntent){ 
    //that means your service is already running 
    // stop your service here 
    context.stopService(newIntent); 
}else{ 
    //Its not running 
    //Start your service here 
    context.startService(newIntent); 
} 

尝试。谢谢

+0

请解释它与现有答案有什么不同,它是如何增加现有问题以解决问题的。 –

+0

这是需要的简短总结 – Fazal