So i'm setting a repeating alarm to fetch data from a server. The problem is that nothing happens when the alarm is suppose to go off. I get no errors while launching it, and the code is correct (as far as i know).
Here is the code that sets the alarm:
private void startRequestTimer() {
// This is what will be called when the alarm goes off (GetOperations)
Intent intent = new Intent(MainActivity.this, GetOperations.class);
PendingIntent operation = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
// The alarm will go off in one second from now
long firstTime = SystemClock.elapsedRealtime();
firstTime += 1000;
// Set the alarm
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 5*1000, operation);
}
Here is GetOperations.java (it's called when the alarm goes off)
package se.jbhalmstad.ndroid;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class GetOperations extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Oh! The alarm went off!", 2000).show();
}
}
And the manifest, in case i messed something up here (which i doubt, since i'm not getting any errors):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.jbhalmstad.ndroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/APP_NAME">
<activity android:name=".MainActivity"
android:label="#string/APP_NAME">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity" />
<activity android:name=".GetOperations" />
</application>
</manifest>
Have i missed something?
You declared GetOperations as a BroadcastReceiver, but in your Manifest it's an Activity.
Related
i was trying to create my own alarm clock in android. The aim is that my alarm clock should play different songs based on my playlist . This is a college project . User sets an alarm and then forgets and then even after multiple restarts the alarm should play . I downloaded a sample code from and was working on this . However on a restart of phone my alarms are getting lost . I found that i need to use broadcast receiver but still its not working on my phone lava phone . Can you please suggest if this code is fine . Here is my code link:https://drive.google.com/file/d/1ADVyH0grEQ4g80HkFU1n2Cb4wxnQC4TX/view?usp=sharing
my android manifest file is..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver" >
</receiver>
</application>
</manifest>
my layout file...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Number of seconds"
android:inputType="numberDecimal" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/time"
android:layout_below="#+id/time"
android:layout_marginRight="60dp"
android:layout_marginTop="120dp"
android:text="Start" />
</RelativeLayout>
my receiver code..
package com.alarmmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;
import android.widget.Toast;
import java.util.Calendar;
/**
* Created by bhaskar on 24-04-2018.
*
*/
public class MyBroadcastReceiver extends BroadcastReceiver {
MediaPlayer mp;
#Override
public void onReceive(Context context, Intent intent) {
mp= MediaPlayer.create(context, R.raw.alrm );
mp.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
Log.i("Alarm","Alarm"+ Calendar.getInstance().getTime());
}
}
mainActivity file
public class MainActivity extends AppCompatActivity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAlert();
}
});
}
public void startAlert() {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();
}
}
see this official doc for maintaining alarm when phone gets restarted.
in Start an Alarm When the Device Boots section
After getting the actual code i am editing this answer :
1) create new Receiver instead of using old one receiver
public class SampleBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Set the alarm here.
Log.d("AlarmDone", "AlarmDone");
}
}
}
2) register it in manifest file :
<receiver
android:name=".SampleBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
3) change your old receiver code in menifest :
<receiver android:name=".AlarmReceiver" />
Note : add permission in manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
now your alarm will ring.
For me it works only when i enable auto start
Can you spot any reason as to why BroadcastReceiver is not being called when the Alarm goes off? If I have the alarm launch an explicit intent, it works just fine and my activity opens. If I set the intent to open my BroadCastReceiver then nothing happens so I think there may be something wrong with my receiver class or the Manifest. Here's how I setup the alarm:
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 324, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Here's my broadcast receiver:
public class AlarmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("MJH", "Alarm called...");
Toast.makeText(context, "Alarm...", Toast.LENGTH_LONG).show();
}
}
And here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="mjh.com.apod"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Required to act as a custom watch face. -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".AlarmBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote">
</receiver>
</application>
Thank you so very much for your time.
I think you should be using PendingIntent.getBroadcast( instead of PendingIntent.getActivity(.
I had to call getBroadcast rather than getActivity to create the PendingIntent. That fixed it.
I am simply trying to implement the AlarmManager. I wrote the code for alarm manager but the code doesnot work. AlarmManager doesnot fire the Broadcast Receiver and service. But when I donot use the AlarmManager and simply start the service using intent the service run. How to make AlarmManager work to schedule the service periodically?
Below is the code:
MainActivity.java
package com.alarmmanager;
import android.app.*;
import android.os.Bundle;
import android.content.*;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent pending =PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),15000,pending);
}
}
AlarmReceiver.java
package com.alarmmanager;
import android.content.*;
public class AlarmReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
intent =new Intent(context,MainService.class);
context.startService(intent);
}
}
MainService.java
package com.alarmmanager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MainService extends Service
{
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
Log.i("nilavs","nilav");
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".MainService"
android:enabled="true"
/>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:process=":remote" android:name=".AlarmReceiver">
</receiver>
</application>
</manifest>
You should use getBroadcast instead of getService?
PendingIntent pending =PendingIntent.getService(this, 0, intent, 0);
Edit:
Try setting it to fire off in the future initially - e.g.
alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+5000,15000,pending);
I am coding a simple alarm test in android, but as mentionned in the title the AlarmReceiver isn't triggered !
i sought here for the solution but none of them worked since my code seems to be correct ...
here is my code :
public class AlarmActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
Button salarm = (Button) findViewById(R.id.salarm);
final AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent i = new Intent(AlarmActivity.this,AlarmReceiver.class);
final PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
salarm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+(1000*5), pi);
Toast.makeText(getApplicationContext(), "Alarm set, wait 5 sec", Toast.LENGTH_SHORT).show();
}
});
}
Manifest file :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidtest3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.androidtest3.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="AlarmActivity"/>
<receiver
android:exported = "true"
android:name = "AlarmReceiver">
</receiver> <!-- <receiver android:name="AlarmReceiver"/> aurait fait l'affaire -->
</application>
</manifest>
AlarmReceiver Class :
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "pending intent received !", Toast.LENGTH_SHORT).show();
}
}
it would help if you could explain the any solution you suggest.
thanx
It's likely not happening because the time value is incorrect. If you are going to use RTC_WAKEUP type alarms, get the time using System.currentTimeMillis() plus your additional time. Right now you're using elapsed time, which is not the same as RTC wall clock time.
I've a class MyBroadcastReceiver that is registered to start at boot up. The manifest is below. I am running this via eclipse on a Android Motorola Xoom wifi only tablet - which is Android 3.2, API level 13 (from the dev.android site).
I have googled for this and tried a few things:
put android:exported="true"
used android:installLocation="internalOnly" to make sure it doesn't install on the SD card
after installing on the device, I ran the app a couple of times before testing the bootup (as some links said Android 3.x onwards won't immediately start sending boot events to the app unless user has manually initiated it at least once)
However even now the broadcast receiver doesn't seem to run when the device boots up (based on logcat).
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypack"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainCamApp" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".UploaderService" />
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Code for broadcast receiver -
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverX extends BroadcastReceiver {
private String TAG = "Broadcast Receiver:";
AlarmManager alarm;
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.i(TAG, "Entered onReceive.");
alarm = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE);
Intent intentUploadService = new Intent (arg0, UploaderService.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
PendingIntent pi = PendingIntent.getBroadcast(arg0, 0, intentUploadService , 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
Log.i(TAG, "Alarm set. Exiting onReceive.");
Intent myIntent = new Intent(arg0, UploaderService.class);
arg0.startService(myIntent);
}
}
Seems to be working now.. final manifest below if it helps -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainCamApp" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".UploaderService" />
<receiver android:name=".MyBroadcastReceiverX" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<category android:name="android.intent.category.DEFAULT"/>
</action>
</intent-filter>
</receiver>
</application>
</manifest>
Try the following:
<receiver android:name=".MyBroadcastReceiverX" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<category android:name="android.intent.category.DEFAULT"/>
</action>
</intent-filter>
</receiver>
You also have incorrect class name specified in manifest - it should be MyBroadcastReceiverX rather then MyBroadcastReceiver
You need to write code after receiving the android.intent.action.BOOT_COMPLETED intent.
try this.
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TEST", intent.getAction());
if (intent.getAction().equalsIgnoreCase(
"android.intent.action.BOOT_COMPLETED")) {
Log.i(TAG, "Entered onReceive.");
alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intentUploadService = new Intent (context, UploaderService.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intentUploadService , 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
Log.i(TAG, "Alarm set. Exiting onReceive.");
//no need to initialize intent twice
// Intent myIntent = new Intent(context, UploaderService.class);
context.startService(intentUploadService );
}
}