Check time every minute by broadcast receiver - java

I want to check the time every minute to make alarm app but when I but this in receiver
<action android:name="android.intent.ACTION_TIME_CHANGED"/>
<action android:name="android.intent.ACTION_TIME_TICK"/>
<action android:name="android.intent.action.TIME_TICK"/>
and put this in broadcast receiver
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"changed",Toast.LENGTH_LONG)
.show();
}
My app don't work or do any thing
I searched more and more but nothing was useful I hope someone help me

From the way you wrote <action android:name="android.intent.ACTION_TIME_CHANGED"/>, it appears that you're trying to register your BroadcastReceiver in the Manifest.xml.
However, I'll quote this straight from the documentation:
ACTION_TIME_TICK
Broadcast Action: The current time has changed. Sent every minute. You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().
--https://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_TICK
You can only receive this broadcast by registering the BroadcastReceiver through the registerReceiver() method in either something like your Activity or Service.
However, I do want to mention that you should avoid using this broadcast if your intention is to create an Alarm app.
Constantly fetching the time every minute can become quite battery draining, especially if the alarm takes a while to ring.
Instead, you should consider scheduling your alarm through services like JobScheduler or AlarmManager.

Related

my app doesn't send any notifications when it's not running (killed)

I have a reminder app that sends a notification according to the time of the item in the listview, the problem is that whenever my phone is rebooted or the app is killed, the app doesn't send any notification.
Note: The app is offline and local, it doesn't use internet connection, I don't use FCM or and online services for this app.
Thank you so much for your time.
Update:
I'm using a thread that searches for data in the local database, If there are any changes in time in the database compared to the current time, the notification should show, but the notification only shows when the app is running, but when the app is killed it doesn't show.
The app needs to run on android 5+,
You can use Broadcast receiver in order to be notified when Boot Completes. And again start the required services of your app.
For reference, have a look here.
This is because when you are killing the app, the onDestroy method gets called. When it's killed, you app is not doing anything. For this you would need a broadcast receiver.
How to implement broadcast receiver?
Create a java class named TimeBradcastReceiver.java.
Paste this code in the class
public class TimeBradcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String dateString = Calendar.getInstance().get(Calendar.HOUR) + ":" + Calendar.getInstance().get(Calendar.MINUTE);
String hourString = Calendar.getInstance().get(Calendar.HOUR);
String minutesString = Calendar.getInstance().get(Calendar.MINUTE;
Log.d("MyBradcastReceiver","i have recieved - " + dateString);
}
}
Once you have implemented this code, you need to add this to you manifest inside the application tag.
<receiver android:name="com.chatverse.free.TimeBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.TIME_TICK"/>
</intent-filter>
</receiver>
Add this code to your activity which opens the first.
IntentFilter mTime = new IntentFilter(Intent.ACTION_TIME_TICK);
registerReceiver(new TimeBradcastReceiver(), mTime);
Now you can do the comparison of the dates and hours and show the notification.
Note - This receiver will update only when a minute has changed.

How do I process broadcasts that were sent while my activity was stopped?

My activity starts a service which runs a CountDownTimer. The timer sends broadcasts back to the activity as it counts down. The activity processes the broadcasts in the onReceive method of a BroadcastReceiver. All of this works fine.
My problem comes when the following events happen in this order:
App is stopped (via onPause())
Timer finishes
App is resumed (via onResume())
When the app is resumed the service is no longer sending broadcasts, so the activity does not know how much time is left on the timer or if it's finished. This prevents the activity from updating the UI.
I've tried a dozen ways of dealing with this, and read through many Stack Overflow questions and answers, but I've yet to find a solution. I would think that there's a way to pick up a broadcast that was sent while the activity was not active, but I've yet to find a way.
For the record, here is my relevant Activity and Service code:
activity.java
// Start service
timerIntent.putExtra("totalLength", totalLength);
this.startService(timerIntent);
// ...
// BroadcastReceiver
private BroadcastReceiver br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getExtras() != null && inSession) {
session.setRemaining(intent.getExtras().getLong("millisUntilFinished"));
updateProgress();
}
}
};
// ...
// onResume
#Override
public void onResume() {
super.onResume();
registerReceiver(br, new IntentFilter(TimerService.COUNTDOWN_TS));
}
service.java
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
long length = intent.getExtras().getLong("totalLength");
countDownTimer = new CountDownTimer(length, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timerServiceIntent.putExtra("millisUntilFinished", millisUntilFinished);
sendBroadcast(timerServiceIntent);
}
#Override
public void onFinish() {
}
};
countDownTimer.start();
return super.onStartCommand(intent, flags, startId);
}
What's the best way to process the broadcasts that the service sent while the activity was stopped?
Use the BroadcastReceiver to store the last request (SharedPreferences perhaps) it received and check it when the Activity starts.
Alternatively, instead of processing a countdown using broadcasts, just store the time that the countdown would end. The Activity can then handle the countdown all by itself as it knows when it should end. Using a service and broadcasts seem to be a little over-engineered for such a simple task.
Update:
From the way you have described your task, I see you needing to handle 2 scenarios. This is how I would likely do it.
Assuming that "XYZ" is the service\intent\whatever starting the countdown and "ABC" is the Activity displaying the progress. (ABC and XYZ could be the same activity if that is what you wanted)
Requirements:
When the countdown starts, I would make XYZ store the time that the countdown should end in SharedPreferences.
ABC is already running when the countdown starts. As Commonsware said, the Eventbus model is excellent for handling this scenario so long as XYZ and ABC are running in the same process. Just fire an event to read the preference value and count down to the specified time. If the user closes ABC and reopens it, Scenario 2 will kick in.
ABC is not running. Check in OnResume whether the countdown time has elapsed. If not, set up ABC to display the countdown again. If there is no countdown active, do something else.
If you also need to do something when the countdown has elapsed regardless of whether you have a UI active, then again Commonsware's suggestion of AlarmManager is perfect.
Let's pretend for a moment that using a Service with a CountDownTimer to track some passage of time for the purposes of updating an Activity actually is a good idea. It's not out of the question, assuming that the Service is actually doing something for real and this timing thing is some by-product.
An activity does not receive broadcasts while stopped, mostly for performance/battery reasons. Instead, the activity needs to pull in the current status when it starts, then use events (e.g., your current broadcasts) to be informed of changes in the data while it is started.
This would be simplified by using something like greenrobot's EventBus and their sticky events, as the activity would automatically get the last event when it subscribes to get events. Using greenrobot's EventBus for this purpose would also reduce the security and performance issues that you are introducing by your use of system broadcasts to talk between two Java classes in the same process.
Also, please stick with lifecycle pairs. onResume() is not the counterpart to onStop(). onStart() is the counterpart to onStop(); onResume() is the counterpart to onPause(). Initializing something in one pair (e.g., onResume()) and cleaning it up in the other pair (e.g., onStop()) runs the risk of double-initialization or double-cleanup errors.
What's the best way to process the broadcasts that the service sent
while the activity was stopped?
Using sticky broadcast intents from the service and then retrieving them from the activity would be a way to process the broadcasts that the service sent while the activity was stopped. I can only offer that as a possible solution rather than claiming it is the "best way".
http://developer.android.com/reference/android/content/Context.html#sendStickyBroadcast(android.content.Intent)
They have however, been deprecated since API level 21 due to security concerns.
Instead of using Normal broadcast you can use Ordered broadcast (sent with Context.sendOrderedBroadcast). For this along with defining a BroadcastReceiver in your activity you required to define BroadcastReceiver in your manifest with same intentfilter. Only change is while registering BroadcastReceiver in your activity you need to set priority to high, so that when your activity is running and activity's BroadcastReceiver is registered it gets called first, and inside onReceive of this BroadcastReceiver you can use abortBroadcast for getting the BroadcastReceiver called which is defined in your android manifest. Now when your activity is not running the BroadcastReceiver defined in your android manifest will get called. So this way you can have the status and if you wish you can display updates to user by notification even if your activity is not running.

How to pause onCreate until boot completed?

I host widgets in my app and it seems, that if i start my app before booting of the device is completed, widgets cannot be created properly. Widgets then seem to be not loaded completely or not initialized/updated correctly. For instance: BatteryBotIndicator-Widget, which shows the battery status in percentage, shows a value of "XX" instead of some number like "70%". If i then restart my app and try to recreate the widget with:
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
it gives me null for appWidgetInfo (i fetch appWidgetId from SQLite database). And widget cannot be recreated.
It is kind of difficult to debug the real cause in this situation to find out what is exactly causing this (Starting Debugger at the right time). I assume the AppWidgetManager is not ready yet or something.
What i can say for sure: If i wait until i receive the broadcast-event BOOT_COMPLETED all widgets are created properly.
So how can i pause the execution of onCreate until booting is completed?
I can think of putting the thread to sleep in a while loop until the BroadcastReceiver (BOOT_COMPLETED) is setting a bool-variable in application-data to true. But i dont want to wait for this event always at starting of my app, as you can imagine ;)
if i start my app before booting of the device is completed, widgets cannot be created properly.
please explain more what exactly do you mean when you say - "not properly" . this might be relevant to understand if you're widget really depends on something related to the boot.
I can think of putting the thread to sleep in a while loop until the BroadcastReceiver (BOOT_COMPLETED) is setting a bool-variable in application-data to true
very bad idea.. also from performances and design reasons
instead, why not simply register to boot complete broadcast from the manifest, and when you recevice it - simply send the relevant broadcast to update your widget? AppWidgetProvider is already extends BroadcastReceiver, so you can simply add it intent filter for boot complete broadcast.
this is how to add intent filter to boot complete:
<receiver android:name="MyWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<meta-data
...
</receiver>
and this is how to react to it from the widget provider implementation:
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
doTheUpdateHereExactlyLikeHowYouUpdateItFromAnywhereElse();
} else {
super.onReceive(context,intent);
}
}
for more information - follow this tutorial :https://laaptu.wordpress.com/2013/08/12/android-update-app-widget-with-listview-after-phone-reboot/

Android - Set notification to never go away

I have this code that works great:
Notification n = builder.build();
n.flags = Notification.FLAG_NO_CLEAR;
But when I restart the phone, the notification goes away. Is there any flag that make that happen?
If you want to print notification when the device boots up, you can create a receiver that is invoked when the system boot is completed, for this, first create a receiver,
public class MyReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("BOOT COMPLETE","SERVICE CALLED>>>>>>>>>>>>");
//use your code here to print notifications
}
}
This receiver is invoked when the system boot is completed. You can also call a service from the onReceive method of receiver to print the notification.
Also you must define the following regularities in your manifest file,
First define permission for getting BOOT_COMPLETION intent,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Then define your receiver also,
<receiver android:name=".MyReciever"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
No. I don't think that is possible.
You could have a service that runs at start-up to to bring up that notification again. Notifications otherwise do not persist across reboots.

Broadcast Receiver issue

Ok, so I have an app that needs to receive incoming SMS, and send out an SMS to the sender. This I can set up fine. The problem is, I only want the Broadcast Receiver to receive when the service is started. I declared the receiver class within the service. I destroy the reference (and unregister the receiver) in onDestroy. While the app compiles, and runs, the broadcast receiver never runs. If I delcare it in the manifest, it works fine, but never stops. The receiver just keeps waking up and processing.
You can try to implement this code:
<receiver android:name=".mystuff" android:enabled="false">
on
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(context, mystuff.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
off
... PackageManager.COMPONENT_ENABLED_STATE_DISABLED ...
One thing to mention is that you need to declare the Receiver file in your manifest for sure. This actually registers your service with the phone. According to API docs
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this
function, the system considers the object to be finished and no longer
active.
So what i have done is i defined my onReceive(Context, Intent) within the Service and register it. Hope this helps.

Categories