Develop Alarm Application
I'd like develop an Alarm Application. The application should work like this: launch it the activity show me the time I can set the alarm I can close the application when the alar
Solution 1:
I have made this application:
AlarmActivity.java
package com.foo;
import pack.breceiver.MyBroadcastReceiver;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
publicclassAlarmActivityextendsActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
publicvoidstartAlert(View view) {
EditTexttext= (EditText) findViewById(R.id.time);
inti= Integer.parseInt(text.getText().toString());
Intentintent=newIntent(this, MyBroadcastReceiver.class);
PendingIntentpendingIntent= PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManageralarmManager= (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();
}
}
MyBroadcastReceiver.java
package pack.breceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;
publicclassMyBroadcastReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
Toast.makeText(context, "Don't panik but your time is up!!!!",
Toast.LENGTH_LONG).show();
/*// Vibrate the mobile phone
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000); */
}
}
Solution 2:
check this out : http://blog.nelsondev.net/?p=124
using"alarmmanager"
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
AlarmCal.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES,
pendingAlarmIntent);
Solution 3:
I had a similar problem and I resolved removing the receiver from manifest and set in my code registering him with registerReceiver.
Solution 4:
Do you get an instantiation exception maybe ?
You should have a public no arg constructor in your service :
publicclassAlarmServiceextendsWakefulIntentService {
publicAlarmService() {
super("AlarmService");
}
// etc
}
Post a Comment for "Develop Alarm Application"