Alarm with broadcast receiver not working on Fragment

旧巷老猫 提交于 2019-12-25 07:58:54

问题


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.



来源:https://stackoverflow.com/questions/24840059/alarm-with-broadcast-receiver-not-working-on-fragment

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