I am attempting to create a more complex app but was unable to correctly use AlarmManager to open an activity. I tried distilling the code down just to the essence of what I'm missing is, but it still is not working correctly.
The app basically just accepts a minute and hour input along with a start button, and is supposed to open the second activity when the time is inputted. However, the second activity never opens at the time I enter, and nothing in LogCat seems to change. Here is my current code:
MANIFEST:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.goali.adalarm">
<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=".AlarmReceiver2" >
<intent-filter>
<action android:name="android.intent.alarmclock.ALARM_ALERT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity:
public class MainActivity extends AppCompatActivity {
Calendar c;
PendingIntent pendingIntent;
AlarmManager alarmManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c = Calendar.getInstance();
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c.set(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour.getText().toString())));
c.set(Calendar.MINUTE, Integer.parseInt(minute.getText().toString()));
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}
2nd activity:
public class AlarmReceiver extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_receiver);
}
}
BroadcastReceiver:
public class AlarmReceiver2 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intent2 = new Intent(context, AlarmReceiver.class);
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
context.startActivity(intent2);
}
}
From you code I guess you want to start an Activity or a Broadcast after amounts of time from now by using AlarmManager.
To start an activity (AlarmReceiver).
public class MainActivity extends AppCompatActivity {
PendingIntent pendingIntent;
AlarmManager alarmManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int hours = Integer.parseInt(hour.getText().toString());
int minutes = Integer.parseInt(minute.getText().toString());
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
// 1 hour = 60 minutes = 3600 seconds = 3600000 milliseconds.
long timeFromNowInMilliseconds = (hours * 3600000) + (minutes * 60000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeFromNowInMilliseconds, pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}
To start a broadcast (AlarmReceiver2)
public class MainActivity extends AppCompatActivity {
PendingIntent pendingIntent;
AlarmManager alarmManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int hours = Integer.parseInt(hour.getText().toString());
int minutes = Integer.parseInt(minute.getText().toString());
Intent intent = new Intent(MainActivity.this, AlarmReceiver2.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
// 1 hour = 60 minutes = 3600 seconds = 3600000 milliseconds.
long timeFromNowInMilliseconds = (hours * 3600000) + (minutes * 60000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeFromNowInMilliseconds, pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}
Related
Can anyone please correct my approach or suggest me the right ways for doing it.I have tried using broadcast receivers in place of services but that approach is not working for it.i.e I used getBroadcast() in place of getService() but didnt work.
It is working when the app is not closed from the application window but as soon as it is closed , the phone doesnt show notifications and ring as it was working in the case when the app wasn't closed.
MainActivity.class
`public class MainActivity extends AppCompatActivity {
TimePicker timePicker;
Button button;
AlarmManager alarmManager;
PendingIntent alarmIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = findViewById(R.id.timePicker);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
if (Build.VERSION.SDK_INT >= 23) {
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
timePicker.getHour(),
timePicker.getMinute(),
0
);
} else {
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute(),
0
);
}
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
//This is the intent which will be fired
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
Log.e("TAG:","AlarmSet.");
// Log is showing the right statement i.e the alarm was set but it wasnt triggered when the app was closed from the application window
}
});
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onPause() {
super.onPause();
Log.e("TAG","OnPause.");
}
#Override
protected void onStop() {
super.onStop();
Log.e("TAG","OnStop.");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.e("TAG","OnDestroy.");
}
}`
IntentService.class
`public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
Log.e("TAG", "Starting service for notification and song");
Random random = new Random();
final Notification notification = new NotificationCompat.Builder(getApplicationContext(), "notify")
.setContentTitle("New Notification")
.setContentText("TIme for task.")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setPriority(Notification.PRIORITY_MAX)
.build();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(random.nextInt(), notification);
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), Settings.System.DEFAULT_RINGTONE_URI);
mediaPlayer.start();
}
}`
Manifest file
`
<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>
<service
android:name=".MyIntentService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>`
I want start my Service after reboot device, I write below codes but when reboot device not start Service.
Manifest:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".alarm1.alarm1"></activity>
<receiver android:name=".alarm1.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<!-- Will not be called unless the application explicitly enables it -->
<receiver android:name=".alarm1.DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Activity code:
public class alarm1 extends AppCompatActivity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm1);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 20;
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 15);
Toast.makeText(alarm1.this, "Start at 17:11", Toast.LENGTH_SHORT).show();
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 3, pendingIntent);
}
}
AlarmReceiver codes:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
DeviceBootReceiver codes:
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
}
}
}
How can I fix this and when reboot device start Service?
Add permissions in Manifest.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Add receiver entry in Manifest.
<receiver android:enabled="true" android:name=".alarm1.DeviceBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
You just need to
Request a permission
and
Explicitly enable the receiver.
The permission goes in the manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Then when the user sets an alarm (for example your start method), enable the boot receiver:
ComponentName receiver = new ComponentName(context, DeviceBootReceiver.class);
getPackageManager().setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
When the user cancels all alarms, you should disable the boot receiver using a similar piece of code.
My alarmmanager code working good. But after boot I can't reschedule my alarm. Can you please tell me where I did mistake?
As you can see in DeviceBootReciever I put the toast. But even this toast doesn't appear. It means I can't listen boot completed.
But I didn't understand the reason. Here I have mentioned my code. Any help will be appreciated!
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(MainActivity.this,AlarmReceiver.class);
pendingIntent= PendingIntent.getBroadcast(MainActivity.this,0,alarmIntent,0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000; // tekrar süresi milisaniye cinsinden
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000; // tekrar süresi milisaniye cinsinden
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int hour = 14;
int min= 43;
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
AlarmReciever.class:
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"AlarmReceiver Calisiyor",Toast.LENGTH_SHORT).show();
}
}
DeviceBootreciever:
public class DeviceBootReceiver extends BroadcastReceiver {
public void onReceive(Context context,Intent intent){
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
}
}
}
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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>
<!-- Will not be called unless the application explicitly enables it -->
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".DeviceBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
I have created a receiver that Extends BroadcastReceiver. This is used to execute my Notification that repeats daily, based on the time selected by the user. For some reason i cant get this to work. I am worried that i am getting the time from the number pickers wrong, but i am new to programing and could use some help. Thank you in advance. Let me know if you see any Errors.
Here is my main activity (MyActivity)
public class MyActivity extends Activity {
TimePicker timePicker;
Button setAlarm;
private int hour;
private int minute;
PendingIntent pendingIntent;
int AM_PM;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
timePicker = (TimePicker) findViewById(R.id.timePicker);
setAlarm = (Button) findViewById(R.id.setUpAlarm);
setAlarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setAlarm();
}
});
}
private void setAlarm() {
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
long time = 60 * hour + minute;
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, NotifyService.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
//create alarms
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
// AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
Here is my Notification class ( extends BroadcatsReceiver )
public class NotifyService extends BroadcastReceiver {
public NotifyService() {
}
#Override
public void onReceive(Context context, Intent intent) {
//generate notification // should be splash activity
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(null)
.setContentText("Your tip for today is ready")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.build();
}
// dont forget to compile "com.android.support:support-v4:18.0.+"
}
Here is my manifest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
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:name=".NotifyService"
android:enabled="true"
android:exported="true" >
</receiver>
</application>
Dont use a receiver with a filter in your android manifest. Create an 'explicit intent' instead of using an intent filter. This way,
Intent i = new Intent(your random intent Action name).
This should fix you problem
Hello I'm new to Java and android, I'm trying to use alarmmanager with a chronometer to display a toast every 10 seconds after the startButton is pressed. Nothing is happening after 10 seconds and I'm not sure why.
Can you look at my code and help me to identify and resolve the problems?
Thankyou.
variables - journeycount = chronometer, pendingIntent mAlarmSender
Main Class - SafeDrive3Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
journeycount = (Chronometer) findViewById(R.id.journeycount);
// Watch for button clicks.
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(mStartListener);
resetButton = (Button) findViewById(R.id.resetButton);
resetButton.setOnClickListener(mResetListener);
}
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
int stoppedMilliseconds = 0;
String chronoText = journeycount.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}
journeycount.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
journeycount.start();
startButton.setText("Driving..."); // changes button text value to 'Driving...'
startButton.setOnClickListener(mStartListener);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
// add 10 seconds to calendar object
cal.add(Calendar.SECOND, 10);
mAlarmSender = PendingIntent.getBroadcast(SafeDrive3Activity.this,
0, new Intent(SafeDrive3Activity.this, MyTimeReceiver.class), 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), mAlarmSender);
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
Button startButton;
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(mStartListener);
journeycount.setBase(SystemClock.elapsedRealtime());
startButton.setText("Start Driving");
journeycount.setText("00:00");
journeycount.stop();
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(mAlarmSender);
}
};
};
Second Class:
public class MyTimeReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "HOUR NOTIFICATION", Toast.LENGTH_LONG).show();
}
}
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Safe"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".SafeDrive3Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name="MyTimeReceiver"></receiver>
</activity>
</application>
</manifest>
I think you should consider a different approach in this scenario. Why not using a Timer that just launch toasts messages?
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
Toast.makeText(SafeDrive3Activity.this, "Whatever", Toast.LENGTH_LONG).show();
}
}, 0, 10000);
use below code set alarm manager
am.set(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime(),mAlarmSender);
Try to use the code below:
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(context, MyService.class);
PendingIntent pi = PendingIntent.getService(context, 0, serviceIntent, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, pi);
I didn't use a BroadcastReviver. I called a Service every 10 seconds.
The new solution:
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(context, MyActivity.class);
PendingIntent pi = PendingIntent.getService(context, 0, serviceIntent, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, pi);
MyActivity:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Toast.makeText(context, "HOUR NOTIFICATION", Toast.LENGTH_LONG).show();
}
}