Android Widget Click and Broadcast Receiver Not Working

非 Y 不嫁゛ 提交于 2019-12-04 07:13:05

It probably works, but you forgot to add .show() at the end of your Toast :)

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

String values are compared using '==' not 'equals'

This "if(intent.getAction()==TEST_INTENT)" change this "if(intent.getAction().equals(TEST_INTENT))"

and of course Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();

All code:

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();
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!