BroadcastReceiver and AlarmManager Android

别说谁变了你拦得住时间么 提交于 2019-11-29 07:32:03

The receiver must extend from BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
    // ...
}

Also be sure that receiver name in manifest file is correct.

your MyBroadcastReceiver class is wrong put this code

public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
     Toast.makeText(context, "Don't panik but your time is up!!!!.",
                Toast.LENGTH_LONG).show();
        // Vibrate the mobile phone
        /*Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);*/       
}
}

just you need to add "extends BroadcastReceiver" and get onReceive() overriding method.

You just forgot to override OnReceive() method. public void onReceive(Context context, Intent intent) is predefined method of BroadcastReceiver, wheneven you are using it, You must @Override it as follows,

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override   // You forgot this line 
    public void onReceive(Context context, Intent intent) 
    {
          Toast.makeText(context, "Don't panik but your time is up!!!!.",Toast.LENGTH_LONG).show();
    }
}

You have created a startAlert function which u didn't call anywhere.So first call that method in onCreate and then you will receive a toast.

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