I've read many tutorials and none of them are making full sense to me. The one I tried had almost no instruction whatsoever, but seemed far simpler than the others. Here is the important code right now:
This is run when a button on the only activity on the app is clicked. The AlarmReciever class looks like this:
The App.getAppContext() was just to retrieve the app context. The class App looks like this:
What I want is for the notification and the vibrator in the AlarmReciever class to trigger immediately when the button is clicked. Yes it's useless, it's just for testing purposes.
What happens though is nothing for a few seconds, then the app crashes while displaying the app stopped running message. The odd thing is the vibrator continues to vibrate the phone until I click ok on the app crashing notification.
Can anyone explain this behavior and what I need to fix to avoid it?
BTW, this is the tutorial: http://www.compiletimeerror.com/2013/10/alarm-example-in-android.html?m=1
Code:
public void startAlarm(View view) {
Intent intent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
}
This is run when a button on the only activity on the app is clicked. The AlarmReciever class looks like this:
Code:
@Override
public void onReceive(Context context, Intent intent) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
NotificationCompat.Builder builder = new NotificationCompat.Builder(App.getAppContext())
.setContentText("The time has come for the notification to trigger...")
.setSmallIcon(R.drawable.notificationicon)
.setLights(Color.rgb(78, 155, 222), 1000, 1000)
.setAutoCancel(true)
.setContentTitle("Your alarm is triggered!");
NotificationManager manager = (NotificationManager) App.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
The App.getAppContext() was just to retrieve the app context. The class App looks like this:
Code:
public class App extends Application {
private static Context mContext;
@Override public void onCreate(){
super.onCreate();
App.mContext = getApplicationContext();
}
public static Context getAppContext(){
return App.mContext;
}
}
What I want is for the notification and the vibrator in the AlarmReciever class to trigger immediately when the button is clicked. Yes it's useless, it's just for testing purposes.
What happens though is nothing for a few seconds, then the app crashes while displaying the app stopped running message. The odd thing is the vibrator continues to vibrate the phone until I click ok on the app crashing notification.
Can anyone explain this behavior and what I need to fix to avoid it?
BTW, this is the tutorial: http://www.compiletimeerror.com/2013/10/alarm-example-in-android.html?m=1