Reputation: 241
I am trying to start an alarm service that repeats every day at a particular time. I have gone through a lot of threads on stack overflow regarding this but no luck. I followed a few tutorials: http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/ and http://javatechig.com/android/repeat-alarm-example-in-android
My service is never started and I do not understand why. Below is my code:
My Manifest file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application>
<service android:name="com.paper.DownloadService" android:enabled="true"/>
<receiver android:name="com.paper.MyReceiver" ></receiver>
</application>
My Receiver Class:
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context rcontext, Intent intent)
{
Log.e("Main Activity", "inside on receive of myreceiver");
Intent service1 = new Intent(rcontext, DownloadService.class);
rcontext.startService(service1);
}
}
My Service Class:
public class DownloadService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.e("Download Service", "CREATED");
}
@SuppressLint({ "SimpleDateFormat", "NewApi" }) @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.e("Download Service", "STARTED");
return START_NOT_STICKY;
}
}
My Main Activity (Inside On Create Method):
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 29);
Intent myIntent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
Here, I am trying to set up my alarm at 3:29 pm every day but the service does not get started at that time or any time for that matter. Any help would be appreciated. Thanks!
Upvotes: 5
Views: 20139
Reputation: 1
Main Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (60 * 1000), pendingIntent); // 60 seconds after the current time
}
}
Alarm Receiver
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("Testing","ok");
}
}
Manifests file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
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=".MyBroadcastReceiver"
android:enabled="true" />
</application>
</manifest>
For background working add this in main activity(if application remove in to recent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
Upvotes: 0
Reputation: 241
Here is what I did to get it working:
1) Added <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
to my manifest file.
2) Changed code in my Main activity to:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 3);
calendar.set(Calendar.MINUTE, 29);
calendar.set(Calendar.AM_PM, Calendar.PM);
Intent myIntent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
Hope someone finds this helpful!
Upvotes: 17