简体   繁体   English

分钟更改时未调用 TIME_TICK

[英]TIME_TICK not called when minute changes

I am facing a problem that when the Time changes the TIME_TICK only called when the app is running.我面临的一个问题是,当时间更改 TIME_TICK 仅在应用程序运行时调用。 But i want to get it called even when app is running or not using broadcast receivers.但即使应用程序正在运行或未使用广播接收器,我也想调用它。

Main 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();
    }
}

Menifest File清单文件

    <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> 

As described in API You have to register this intent programmatically:如 API 中所述,您必须以编程方式注册此意图:

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().您无法通过清单中声明的​​组件接收此信息,只能通过使用 Context.registerReceiver() 显式注册它。

You have to register it for example in Your mainActivity:例如,您必须在您的 mainActivity 中注册它:

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

According to the Android documentation :根据Android 文档

You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver()您无法通过清单中声明的​​组件接收此信息,只能通过使用 Context.registerReceiver() 显式注册它

Looking to your code, something like:查看您的代码,例如:

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();

      receiver = new MyBroadastReceivers();
      registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_TICK));
      }

      @Override
      protected void onDestroy() {
      // Unregister MyBroadastReceivers
      unregisterReceiver(receiver);
      }
    } 

Plus, depending on what you want to do, AlarmManager may be a good alternative "particularly if you let the user configure the polling frequency".另外,根据您想要做什么,AlarmManager 可能是一个不错的选择,“特别是如果您让用户配置轮询频率”。 Please take a look in this this SO thread .请看看这个SO 线程

PErfact Example i have tested and worked perfactly PERfact 示例我已经测试并完美地工作

 package com.sifat.githubalarmapp; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "I am running sifat", Toast.LENGTH_LONG).show(); } }

 package com.sifat.githubalarmapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); } }

Add in manifest在清单中添加

 <!-- Will not be called unless the application explicitly enables it --> <receiver android:name=".AlarmReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.ACTION_TIME_TICK"/> </intent-filter> </receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM