I am learning how to use the AlarmManager to set up notifications but when I run this, the app crashes, not sure why. Could anyone take a look at it and see what's wrong, please?
This is my MainActivity.java file
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity
{
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
calendar.get(Calendar.MONTH);
calendar.get(Calendar.YEAR);
calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 50);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
} //end onCreate
}
this is my MyReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
This is my AlarmService.java file
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyAlarmService extends Service
{
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification;
CharSequence from="AlarmDemo";
CharSequence message="This is a test message!";
//= new Notification(R.mipmap.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder=new Notification.Builder(MyAlarmService.this);
builder.setAutoCancel(false);
builder.setContentTitle(from);
builder.setContentText(message);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(pendingNotificationIntent);
builder.build();
notification=builder.getNotification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mManager.notify(0, notification);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
And this is my AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
<service android:name=".MyAlarmService"
android:enabled="true" />
<receiver android:name=".MyReceiver"/>
</application>
This is what my logcat says
09-10 15:09:36.259 7105-7129/com.example.androidalarmmanagerdemo E/eglCodecCommon﹕ writeFully: failed: Broken pipe
09-10 15:09:36.259 7105-7129/com.example.androidalarmmanagerdemo W/libEGL﹕ eglInitialize(0xa3788040) failed (EGL_SUCCESS)
09-10 15:09:36.259 7105-7129/com.example.androidalarmmanagerdemo I/OpenGLRenderer﹕ Initialized EGL, version 1.4
09-10 15:09:36.259 7105-7129/? E/EGL_emulation﹕ tid 7129: eglChooseConfig(533): error 0x3001 (EGL_NOT_INITIALIZED)
09-10 15:09:36.259 7105-7129/? W/OpenGLRenderer﹕ Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
09-10 15:09:36.259 7105-7129/? E/EGL_emulation﹕ tid 7129: eglChooseConfig(533): error 0x3001 (EGL_NOT_INITIALIZED)
09-10 15:09:36.259 7105-7129/? A/OpenGLRenderer﹕ Failed to choose config, error = EGL_NOT_INITIALIZED
09-10 15:09:36.259 7105-7129/? A/libc﹕ Fatal signal 4 (SIGILL), code 2, fault addr 0xb74be62c in tid 7129 (RenderThread)
This fatal signal occurs while rendering the app and appears have nothing to do with your code. If you are running on an Emulator, try to create another one and see what happens.