2010-12-01 7 views
5

नीचे दिए गए कोड को उस ऐप का वर्णन करना चाहिए जहां एक बार विजेट बटन क्लिक किया जाता है, यह टेस्ट रिसीवर द्वारा प्राप्त किया जाने वाला एक इरादा भेजता है। हालांकि, मेरे नीचे दिए गए कोड को चलाने में, टेस्ट रिसीवर के ऑनसेसिव को कभी नहीं कहा जाता है।एंड्रॉइड विजेट क्लिक और ब्रॉडकास्ट रिसीवर काम नहीं कर रहा है

क्या कोई मुझे बता सकता है कि मैं क्या गलत कर रहा हूं?

विजेट कोड

public class Widget extends AppWidgetProvider { 

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    final int N = appWidgetIds.length; 

    // Perform this loop procedure for each App Widget that belongs to this provider 
    for (int i=0; i<N; i++) { 
     int appWidgetId = appWidgetIds[i]; 

     // Create an Intent to launch ExampleActivity 
     //Intent intent = new Intent(context.getApplicationContext(), TestReceiver.class); 
     Intent intent = new Intent(); 
     intent.setAction(TestReceiver.TEST_INTENT); 
     intent.setClassName(TestReceiver.class.getPackage().getName(), TestReceiver.class.getName()); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

     // Get the layout for the App Widget and attach an on-click listener to the button 
     RemoteViews views; 

     views = new RemoteViews(context.getPackageName(), R.layout.main);  

     views.setOnClickPendingIntent(R.id.btnTest, pendingIntent); 

     // Tell the AppWidgetManager to perform an update on the current App Widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 



    } 


} 

}

रिसीवर कोड:

public class TestReceiver extends BroadcastReceiver { 

    public static final String TEST_INTENT= "MyTestIntent"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     Toast.makeText(context, "Test", Toast.LENGTH_SHORT); 

     if(intent.getAction()==TEST_INTENT) 
     { 
     System.out.println("GOT THE INTENT"); 

     Toast.makeText(context, "Test", Toast.LENGTH_SHORT); 
     } 
    } 

    } 

मैनिफ़ेस्ट:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.test.intenttest" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <receiver android:name=".TestReceiver" android:label="@string/app_name"> 
    <intent-filter> 
    <action android:name="MyTestIntent"> 
    </action> 
    </intent-filter> 
    </receiver> 
    <receiver android:label="@string/app_name" android:name="Widget"> 
    <intent-filter> 
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
    </intent-filter> 
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" /> 
    </receiver> 
    </application> 
    <uses-sdk android:minSdkVersion="3" /> 

</manifest> 
+0

बेवकूफ सवाल पहले - क्या यह काम करता है यदि आप केवल एक नियमित इरादा() बनाते हैं और अपने ऐप में कहीं भी प्रारंभ एक्टिविटी() को कॉल करते हैं? बस यह सुनिश्चित करना कि रिसीवर सही तरीके से स्थापित है। – EboMike

+0

मैंने context.sendBroadcast (इरादा) जोड़ा; विजेट में अद्यतन अद्यतन पर। ऐसा लगता है कि अब यह डिबगिंग कर रहा है, ऐसा लगता है कि वह उस कथन पर रिसीवर को कॉल कर रहा है और अब जब मैं बटन पर क्लिक कर रहा हूं। मुझे लगता है कि मैं उलझन में था क्योंकि टोस्ट कॉल मैं कुछ भी नहीं कर रहा था। – Kratz

+0

हाँ, क्योंकि आपने '.show()' :) – EboMike

उत्तर

7

यह शायद काम करता है, लेकिन आप पर .show() जोड़ने के लिए भूल गया आपके टोस्ट का अंत :)

0

== संदर्भ समानता के लिए परीक्षण (चाहे वे एक ही वस्तु हैं)।

.equals() मूल्य समानता के लिए परीक्षण (चाहे वे तार्किक रूप से "बराबर" हों)।

स्ट्रिंग मान का उपयोग कर तुलना की जाती है '==' नहीं 'के बराबर है'

यह "if(intent.getAction()==TEST_INTENT)" इस "if(intent.getAction().equals(TEST_INTENT))"

और निश्चित रूप से Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();

सभी कोड बदलने के लिए:

package *********; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 


public class TestReceiver extends BroadcastReceiver { 

    public static final String TEST_INTENT= "MyTestIntent"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     Toast.makeText(context, "Test holaaa", Toast.LENGTH_SHORT).show(); 

     if(intent.getAction() == TEST_INTENT) 
      // if(intent.getAction().equals(TEST_INTENT)) 
     { 
      System.out.println("GOT THE INTENT"); 

      Toast.makeText(context, "Test Goooo", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
संबंधित मुद्दे