广播接收器问题,唤醒广播接收器

时间:2021-01-22 10:01:37

标签: java android broadcastreceiver android-broadcast

我在我的应用中使用带有闹钟管理器的唤醒广播接收器来在特定时间执行一些任务。 它运作良好。但我面临一个小问题 例如,我在晚上 9:00 设置我的闹钟管理器,如果我从最近的应用程序中删除我的应用程序,它肯定会在后台运行,但是在特定时间(晚上 9:00)之后,每当我使用我的应用程序时,我的闹钟都会立即触发. 我希望每个人(有关这个问题)都能理解我的观点。 如果有人知道这个问题,请告诉我。 谢谢。

在这里我正在注册我的接收器

        <receiver
        android:name="com.example.chartdemo.MyBroadcastReceiver"
        android:enabled="true"
        android:exported="true">

       </receiver>

这是我的 MainActivity.java 代码

public class MainActivity extends AppCompatActivity {
public  static TextView textView,textView1;
Button button, button1,button2,button3;
public static int counter=0, time;
MyBroadcastReceiver myBroadcastReceiver;
myDbAdapter helper;
//private final MyDateChangeReceiver mDateReceiver = new MyDateChangeReceiver();
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.text);
        textView1=findViewById(R.id.text1);
        button=findViewById(R.id.counterbtn);
        button1=findViewById(R.id.showbtn);
        button2=findViewById(R.id.savebtn);
        button3=findViewById(R.id.delbtn);
        helper = new myDbAdapter(this);

        loadData(); //for data retrieving from shared preferences


        startAlarm(this);
    }


private void startAlarm(Context context) {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 9);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(AM_PM, PM);
        time= (int) cal.getTimeInMillis();

        AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(context, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 234, intent, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
            Toast.makeText(context, "alarm set after "+cal.getTimeInMillis(), Toast.LENGTH_SHORT).show();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
            Toast.makeText(context, "alarm set after "+cal.getTimeInMillis(), Toast.LENGTH_SHORT).show();
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
            Toast.makeText(context, "alarm set after "+cal.getTimeInMillis(), Toast.LENGTH_SHORT).show();
        }


    }

这是我的 WakefulBroadcastReceiver.java 代码

 public class MyBroadcastReceiver extends WakefulBroadcastReceiver  {
    MediaPlayer mp;
    myDbAdapter helper;
    MainActivity mainActivity=new MainActivity();


        @Override
        public void onReceive(Context context, Intent intent) {

                SharedPreferences sh = context.getSharedPreferences("MySharedPref", MODE_PRIVATE);
                int a = sh.getInt("counter", 0);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String date = sdf.format(new Date());
                helper = new myDbAdapter(context);
                long id = helper.insertData(date, String.valueOf(a));
                if (id <= 0) {
                Toast.makeText(context, "Insertion UnSuccessful", Toast.LENGTH_SHORT).show();
                } else {
                Toast.makeText(context, "Insertion Successful", Toast.LENGTH_SHORT).show();
                }
                mp = MediaPlayer.create(context, R.raw.alarm);
                mp.start();
                Toast.makeText(context, "Alarm...!!!!!! ", Toast.LENGTH_LONG).show();
                SharedPreferences sharedPreferences = context.getSharedPreferences("MySharedPref", MODE_PRIVATE);
                SharedPreferences.Editor myEdit = sharedPreferences.edit();
                myEdit.putInt("counter", 0);
                myEdit.commit();

        }

0 个答案:

没有答案
相关问题