2010-12-01 50 views
5

下面的代碼應該描述一個應用程序,一旦點擊了Widget按鈕,它就發送一個應該由TestReceiver接收的意圖。但是,在運行我的下面的代碼時,TestReceiver的onReceive從不會被調用。Android Widget點擊和廣播接收器不工作

有人能讓我知道我在做什麼錯嗎?

部件代碼

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

第一個愚蠢的問題 - 如果你只是在你的應用程序的某處創建一個常規的Intent()並調用startActivity(),它會起作用嗎?只要確保接收機設置正確。 – EboMike 2010-12-01 21:45:36

回答

7

它可能的工作,但你忘了加.show()在Toast結束:)

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(); 
     } 
    } 
}