2010-12-01 7 views
5

以下のコードでは、ウィジェットボタンをクリックすると、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

+0

私はcontext.sendBroadcast(intent)を追加しました。ウィジェットのonUpdate関数に渡します。今すぐデバッグすると、そのステートメントの受信者を呼び出すようになり、今すぐボタンをクリックしているようです。私が作っていたトーストの呼び出しは何もしないので、私は混乱していたと思う。 – Kratz

+0

ええ、あなたが '.show()'を追加しなかったので、 – EboMike

答えて

7

それはおそらく動作しますが、あなたはで.show()を追加するのを忘れあなたのトーストの終わり:

0

==それらが同じオブジェクトであるかどうかをテストします。

.equals()は値の等しいかどうかをテストします(論理的に "等しい"かどうか)。

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

文字列値は、これは "if(intent.getAction()==TEST_INTENT)" この "if(intent.getAction().equals(TEST_INTENT))"

そしてもちろんToast.makeText(context, "Test", Toast.LENGTH_SHORT).show();

すべてのコードを変更する '==' '等しい' ではない

を使用して比較しています

関連する問題