在一段时间内禁用无线电组 (Android)
Disable Radio groups for certain period of time (Android)
我在自定义列表视图中有 3 个广播组。
我需要从早上 9 点到下午 1 点启用第一个无线电组,然后禁用。第二个无线电组从下午 1 点到晚上 8 点启用,之后禁用,第三个无线电组从晚上 8 点到早上 8 点启用,之后禁用。我怎样才能达到同样的效果??
BroadCastReceiver.java
public class BackgroundService extends Service {
private static BroadcastReceiver mTickReceiver;
public BackgroundService()
{
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate()
{
registerReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(mTickReceiver);
mTickReceiver = null;
}
private void registerReceiver()
{
mTickReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) // this will fire every minute
{
String shift = null;
DateFormat df = new SimpleDateFormat("HH:mm");
String currentTimeString = df.format(Calendar.getInstance().getTime());
Log.d("CurrentTime",currentTimeString);
int currentTime=convertTimeToMinutes(currentTimeString);
Log.d("CurrentTime1", String.valueOf(currentTime));
Intent i=new Intent();
i.setAction("TimeData");
if(currentTime>=480 && currentTime<780) {
shift = "Shift1";
}
else if(currentTime>=780 && currentTime<1200) {
shift = "Shift2";
}
else if(currentTime>=1200 || currentTime<480) {
shift = "Shift3";
}
Log.d("TAG1",shift);
intent.putExtra("Shift",shift);
sendBroadcast(i);
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK); // this will broadcast Intent every minute
registerReceiver(mTickReceiver, filter);
}
private int convertTimeToMinutes(String s) {
String[] hourMin = s.split(":");
int hour = Integer.parseInt(hourMin[0]);
int mins = Integer.parseInt(hourMin[1]);
int hoursInMins = hour * 60;
return hoursInMins + mins;
}
}
在 Activity onCreate 方法中,我按如下方式调用 BroadcastReceiver
Intent i=new Intent(this, BackgroundService.class);
startService(i);
IntentFilter filter = new IntentFilter("TimeData");
registerReceiver(new MyBroadcastReceiver(),filter);
BroadCastReceiver Class in Activity如下
class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// GET BROADCAST FROM RECEIVER IN THE BACKGROUND SERVICE CLASS
Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show();
Log.d("ActionData",intent.getAction()); // this works, it is checked
if (intent.getAction() == "TimeData") {
String shiftValue1= intent.getStringExtra("Shift");
Log.d("shiftValue1",shiftValue1);
}
}
}
我得到的错误是
FATAL EXCEPTION: main
Process: com.rakyow.ehandor, PID: 11492
java.lang.RuntimeException: Error receiving broadcast Intent { act=TimeData flg=0x10 } in com.rakyow.ehandor.HandoverDetailActivity$MyBroadcastReceiver@1acc203e
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:917)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5576)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:139)
at com.rakyow.ehandor.HandoverDetailActivity$MyBroadcastReceiver.onReceive(HandoverDetailActivity.java:290)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:907)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5576)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
对于这种功能,您可以创建定时器服务来检查特定时间段的时间,或者您可以使用警报服务 将在特定时间触发。
这是你每分钟检查时间的方式:
public class BackgroundService extends Service {
private static BroadcastReceiver mTickReceiver;
public BackgroundService()
{
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate()
{
registerReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(mTickReceiver);
mTickReceiver = null;
}
private void registerReceiver()
{
mTickReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) // this will fire every minute
{
Log.d("TAG", "TICKS");
// Check current time using getTimeInMilliSeconds()
// if time is in your desired range, send UI an intent that it should change radio buttons
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK); // this will broadcast Intent every minute
registerReceiver(mTickReceiver, filter);
}
}
How to send info/data from a receiver in a service to MainActivity,您的案例信息要更新 UI。
在上面的示例中,我传递了位置数据,您可以在检查时间范围后传递任何信息。
更新:
使用 SimpleDateFormat
将毫秒转换为小时字符串(例如 1481879288 是“09:08am”(格林威治标准时间)。
写一个函数,把HH:mm[am/pm]格式转换成分钟——“09:08am”转换成728分钟,或者说“02:35pm” 875 分钟,这是从 12:00am 参考点开始的,其中 12:00am 是 0 分钟过去了。 (Hint: break string, convert hours to minutes, add minutes part to converted one, write checks for am/pm)
提示提示 - 如果将毫秒转换为 24 小时格式,您将避免检查 am/pm,只需乘以 60xhourPart 并添加分钟部分。
将分钟范围与您的范围进行比较,例如从 08:00am 到 08:00pm,这将是 480 分钟到 1200 分钟,所以如果当前时间是 09:08am,检查将是:
int currentTime=convertTimeToMinutes(currentTimeString);
// to check if time is between 08:00am to 08:00pm
if(currentTime>=480 && currentTime<=1200) {
Log.d(TAG,"Current time is in between 08:00am to 08:00pm");
}
How if the range is between 8pm-8am(Next day happens)??
如果是这种情况,请使用检查来查看时间是大于晚上 8 点还是小于早上 8 点,注意这里检查的是 ||不是 &&。
if (currentTime>=1320 || currentTime <=480)
{
// your stuff
}
更新 2
String
比较必须始终使用 .equals()
方法而不是 ==
,因为您使用 ==
,行
String shiftValue1= intent.getStringExtra("Shift")
从不执行,你应该写成:
if (intent.getAction().equals("TimeData")) {
String shiftValue1= intent.getStringExtra("Shift");
Log.d("shiftValue1",shiftValue1);
}
更新 3
不要这样做:
intent.putExtra("Shift",shift);
这样做:
i.putExtra("Shift",shift);
看到您正在发送与收到的相同的意图对象。
我在自定义列表视图中有 3 个广播组。 我需要从早上 9 点到下午 1 点启用第一个无线电组,然后禁用。第二个无线电组从下午 1 点到晚上 8 点启用,之后禁用,第三个无线电组从晚上 8 点到早上 8 点启用,之后禁用。我怎样才能达到同样的效果??
BroadCastReceiver.java
public class BackgroundService extends Service {
private static BroadcastReceiver mTickReceiver;
public BackgroundService()
{
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate()
{
registerReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(mTickReceiver);
mTickReceiver = null;
}
private void registerReceiver()
{
mTickReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) // this will fire every minute
{
String shift = null;
DateFormat df = new SimpleDateFormat("HH:mm");
String currentTimeString = df.format(Calendar.getInstance().getTime());
Log.d("CurrentTime",currentTimeString);
int currentTime=convertTimeToMinutes(currentTimeString);
Log.d("CurrentTime1", String.valueOf(currentTime));
Intent i=new Intent();
i.setAction("TimeData");
if(currentTime>=480 && currentTime<780) {
shift = "Shift1";
}
else if(currentTime>=780 && currentTime<1200) {
shift = "Shift2";
}
else if(currentTime>=1200 || currentTime<480) {
shift = "Shift3";
}
Log.d("TAG1",shift);
intent.putExtra("Shift",shift);
sendBroadcast(i);
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK); // this will broadcast Intent every minute
registerReceiver(mTickReceiver, filter);
}
private int convertTimeToMinutes(String s) {
String[] hourMin = s.split(":");
int hour = Integer.parseInt(hourMin[0]);
int mins = Integer.parseInt(hourMin[1]);
int hoursInMins = hour * 60;
return hoursInMins + mins;
}
}
在 Activity onCreate 方法中,我按如下方式调用 BroadcastReceiver
Intent i=new Intent(this, BackgroundService.class);
startService(i);
IntentFilter filter = new IntentFilter("TimeData");
registerReceiver(new MyBroadcastReceiver(),filter);
BroadCastReceiver Class in Activity如下
class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// GET BROADCAST FROM RECEIVER IN THE BACKGROUND SERVICE CLASS
Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show();
Log.d("ActionData",intent.getAction()); // this works, it is checked
if (intent.getAction() == "TimeData") {
String shiftValue1= intent.getStringExtra("Shift");
Log.d("shiftValue1",shiftValue1);
}
}
}
我得到的错误是
FATAL EXCEPTION: main Process: com.rakyow.ehandor, PID: 11492 java.lang.RuntimeException: Error receiving broadcast Intent { act=TimeData flg=0x10 } in com.rakyow.ehandor.HandoverDetailActivity$MyBroadcastReceiver@1acc203e at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:917) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5576) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) Caused by: java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.d(Log.java:139) at com.rakyow.ehandor.HandoverDetailActivity$MyBroadcastReceiver.onReceive(HandoverDetailActivity.java:290) at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:907) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5576) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
对于这种功能,您可以创建定时器服务来检查特定时间段的时间,或者您可以使用警报服务 将在特定时间触发。
这是你每分钟检查时间的方式:
public class BackgroundService extends Service {
private static BroadcastReceiver mTickReceiver;
public BackgroundService()
{
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate()
{
registerReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(mTickReceiver);
mTickReceiver = null;
}
private void registerReceiver()
{
mTickReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) // this will fire every minute
{
Log.d("TAG", "TICKS");
// Check current time using getTimeInMilliSeconds()
// if time is in your desired range, send UI an intent that it should change radio buttons
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK); // this will broadcast Intent every minute
registerReceiver(mTickReceiver, filter);
}
}
How to send info/data from a receiver in a service to MainActivity,您的案例信息要更新 UI。
在上面的示例中,我传递了位置数据,您可以在检查时间范围后传递任何信息。
更新:
使用
SimpleDateFormat
将毫秒转换为小时字符串(例如 1481879288 是“09:08am”(格林威治标准时间)。写一个函数,把HH:mm[am/pm]格式转换成分钟——“09:08am”转换成728分钟,或者说“02:35pm” 875 分钟,这是从 12:00am 参考点开始的,其中 12:00am 是 0 分钟过去了。
(Hint: break string, convert hours to minutes, add minutes part to converted one, write checks for am/pm)
提示提示 - 如果将毫秒转换为 24 小时格式,您将避免检查 am/pm,只需乘以 60xhourPart 并添加分钟部分。将分钟范围与您的范围进行比较,例如从 08:00am 到 08:00pm,这将是 480 分钟到 1200 分钟,所以如果当前时间是 09:08am,检查将是:
int currentTime=convertTimeToMinutes(currentTimeString); // to check if time is between 08:00am to 08:00pm if(currentTime>=480 && currentTime<=1200) { Log.d(TAG,"Current time is in between 08:00am to 08:00pm"); }
How if the range is between 8pm-8am(Next day happens)??
如果是这种情况,请使用检查来查看时间是大于晚上 8 点还是小于早上 8 点,注意这里检查的是 ||不是 &&。
if (currentTime>=1320 || currentTime <=480)
{
// your stuff
}
更新 2
String
比较必须始终使用 .equals()
方法而不是 ==
,因为您使用 ==
,行
String shiftValue1= intent.getStringExtra("Shift")
从不执行,你应该写成:
if (intent.getAction().equals("TimeData")) {
String shiftValue1= intent.getStringExtra("Shift");
Log.d("shiftValue1",shiftValue1);
}
更新 3
不要这样做:
intent.putExtra("Shift",shift);
这样做:
i.putExtra("Shift",shift);
看到您正在发送与收到的相同的意图对象。