Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
I am new in android, I want to create an application that alerts you and gives you a notification when its time comes, and I want that application to still run in the background even you closed the interface.
ex:
if you set the date to be 21-03-2014 and set the time to 10:30 AM, when this date and time come, the application will running automatically and alarming you and appears a message to remind you to do some thing.

Can you help me, please?
Posted

1 solution

You can run a Service[^] and start a Timer[^] to trigger notification.
 
Share this answer
 
Comments
fadi77_net 4-Nov-13 3:20am    
which service??
can you be more clear please
Xiao Ling 4-Nov-13 20:08pm    
Service is like Activity which is the component of Application. You can create a service running in background. When you create a service in you application, you have to declare it in AndroidManifest.xml. Here is the sample code of Service:
public class LocalService extends Service {
private NotificationManager mNM;

// Unique Identification Number for the Notification.
// We use it on Notification start, and to cancel it.
private int NOTIFICATION = R.string.local_service_started;

/**
* Class for clients to access. Because we know this service always
* runs in the same process as its clients, we don't need to deal with
* IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}

@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

// Display a notification about us starting. We put an icon in the status bar.
showNotification();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}

@Override
public void onDestroy() {
// Cancel the persistent notification.
mNM.cancel(NOTIFICATION);

// Tell the user we stopped.
Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();

/**
* Show a notification while this service is running.
*/
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.local_service_started);

// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.stat_sample, text,
System.currentTimeMillis());

// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, LocalServiceActivities.Controller.class), 0);

// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label),
text, contentIntent);

// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
}
fadi77_net 9-Nov-13 16:44pm    
dear
can you send me a complete sample for what I want?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900