2013-07-07 10 views
5

से मैं विजेट के अंदर से एक सेवा शुरू करना चाहते हैं, मुझे पता है कि मैं इसेआरंभ/सेवा रोकें विजेट

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

तरह PendingIntent उपयोग कर सकते हैं, लेकिन समस्या यह है कि मैं पहले से ही उस पर 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); 
    } 
} 

नोट::

यहाँ मेरी कोड है अगर मैं startService और stopService युक्त लाइनों को हटाने, सब कुछ सुचारू रूप से चल रहा है।

अग्रिम में धन्यवाद !! :)

+0

यदि आप 'context.startService (नया इरादा (संदर्भ, MyService.class)) का उपयोग करते हैं, तो यह 'वर्चुअल' सेवा इन्टेंट 'के बजाय' काम करता है? –

+0

@ नील टाउनसेंड: धन्यवाद सर, यह काम किया। बहुत बहुत धन्यवाद!! क्या आप कृपया समझा सकते हैं कि यह पहले क्यों काम नहीं कर रहा था ?? एक बार फिर धन्यवाद। :) –

+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

संबंधित मुद्दे