简体   繁体   中英

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

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

You have to register it for example in Your mainActivity:

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

According to the Android documentation :

You can not receive this through components declared in manifests, only by explicitly registering for it with 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". Please take a look in this this SO thread .

PErfact Example i have tested and worked perfactly

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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