gpt4 book ai didi

java - 分钟更改时不调用 TIME_TICK

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:47:10 24 4
gpt4 key购买 nike

我遇到的问题是,当时间更改时,TIME_TICK 仅在应用程序运行时调用。但我想在应用程序正在运行或不使用广播接收器时调用它。

主要 Activity

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "Checking", Toast.LENGTH_LONG).show();
}
}

MyBroadastReceivers

public class MyBroadastReceiversextends BroadcastReceiver
{

@Override
public void onReceive(Context arg0, Intent arg1) {

Toast.makeText(arg0, "Toast Receive", Toast.LENGTH_LONG).show();
}
}

list 文件

    <receiver android:name="com.example.serviceschecking.MyBroadastReceivers" android:enabled="true" android:exported="true">  
<intent-filter >
<action android:name="android.intent.action.TIME_TICK"/>
</intent-filter>
</receiver>

最佳答案

如 API 中所述,您必须以编程方式注册此 Intent :

Broadcast Action: The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().

例如,您必须在您的 mainActivity 中注册它:

      IntentFilter mTime = new IntentFilter(Intent.ACTION_TIME_TICK);
registerReceiver(YourReceiver, mTime);

关于java - 分钟更改时不调用 TIME_TICK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30209028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com