Alarm with broadcast receiver not working on Fragm

2019-08-31 02:59发布

问题:

I'm working on a app with Fragments, One of the fragment includes alarm functionality. For this I have used broadcast receiver but don't understand why its not working: Here is the code, I'm working on PC "not a Mobile device"

public class SchedulerListFragment extends Fragment {
    PendingIntent pendingIntent;
    AlarmManager alarmManager;
    BroadcastReceiver mReceiver;
    EditText ethr,etmin,etsec;
    int result = 1;
    int hr = 0;
    int min = 0;
    int sec = 0;
    Button alarm;
    IntentFilter filter1;

    private void RegisterAlarmBroadcast()
    {       
        mReceiver = new BroadcastReceiver()
        {
           // private static final String TAG = "Alarm Example Receiver";
            @Override
            public void onReceive(Context context, Intent intent)
            {
                Toast.makeText(context, "Alarm time has been reached", Toast.LENGTH_LONG).show();
            }
        };

        getActivity().registerReceiver(mReceiver, new IntentFilter("com.example.lightcontrolsystem.SchedulerFragment.class") );
        pendingIntent = PendingIntent.getBroadcast( getActivity(), 0, new Intent("sample"),0 );
        alarmManager = (AlarmManager)(getActivity().getSystemService( Context.ALARM_SERVICE ));
    }


    /*
    private void UnregisterAlarmBroadcast()
    {
        alarmManager.cancel(pendingIntent); 
        getBaseContext().unregisterReceiver(mReceiver);
    }
*/

    Button.OnClickListener buttonalarmOnClickListener = new Button.OnClickListener() {

        public void onClick(View v) {
             String shr = ethr.getText().toString();
                if(shr.equals(""))
                {
                    hr = 0;
                }
                else
                {
                    hr = Integer.parseInt(ethr.getText().toString());
                    hr=hr*60*60*1000;
                }

                String smin = etmin.getText().toString();
                if(smin.equals(""))
                {
                    min = 0;
                }
                else
                {
                     min = Integer.parseInt(etmin.getText().toString());
                     min = min*60*1000;

                }

                String ssec = etsec.getText().toString();
                if(ssec.equals(""))
                {
                    sec = 0;
                }
                else
                {
                     sec = Integer.parseInt(etsec.getText().toString());
                     sec = sec * 1000;

                }

                result = 50*1000; //hr+min+sec;
                Toast.makeText(getActivity(), "Alarm SET", Toast.LENGTH_LONG).show();
                alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + result , pendingIntent);
        //      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), result , pendingIntent); 


        }
    }; 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.scheduler_list, container,false);
        ethr = (EditText) view.findViewById(R.id.ethr);
        etmin = (EditText) view.findViewById(R.id.etmin);
        etsec = (EditText) view.findViewById(R.id.etsec);
        alarm = (Button) view.findViewById(R.id.setAlarm);
        alarm.setOnClickListener(buttonalarmOnClickListener);


        RegisterAlarmBroadcast();

                return view; 
    }
}

Problem1: Alarm not generated I suspect I have not registered Broadcast receiver properly

  getActivity().registerReceiver(mReceiver, new IntentFilter("com.example.lightcontrolsystem.SchedulerFragment.class") );
        pendingIntent = PendingIntent.getBroadcast( getActivity(), 0, new Intent("sample"),0 );
        alarmManager = (AlarmManager)(getActivity().getSystemService( Context.ALARM_SERVICE ));

Problem2: EditText filed not showing any keypad or responding to any of my keys from PC. Don't know Why

Thanks in advance

回答1:

Why don't you try a custom BroadcastReceiver in place of your inline implementation? Try this

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm time has been reached", Toast.LENGTH_LONG).show();
    }
}

Register the receiver in AndroidManifest

<receiver android:name=".AlarmReceiver" android:process=":remote"/>

set the alarm in your SchedulerListFragment.java

Intent intent = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);

AlarmManager alarmManager = (AlarmManager)(getActivity().getSystemService( Context.ALARM_SERVICE ));
alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + result , pendingIntent);

If you wish, you may register the BroadcastReceiver dynamically using registerReceiver() by passing an empty IntentFilter. That will also work without any issues.