Well, if your application queries an external database, you can, instead of doing a thread, do a service that does just what you said.
The difference is that even with the application closed and the user doing other things or even with the screen off, you can notify them that a new message exists.
Be aware that as your application performs the query in an external database, the check range may be one or more times.
Example service:
public class MessageVerifyService extends Service
{
private Handler serviceHandler;
private Task myTask;
NotificationManager notify;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
notify = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
myTask = new Task();
serviceHandler = new Handler();
serviceHandler.postDelayed(myTask,1000);
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
try
{
serviceHandler.removeCallbacks(myTask);
serviceHandler = null;
}
catch(Exception e)
{
}
}
public void showNotificationAlert(int numMensagens)
{
Intent intent= new Intent(getBaseContext(), MainActivity.class); // coloque sua activity para ver as mensagens não lidas
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
Notification note = new Notification(R.drawable.ic_launcher,"Mensagens não Lidas",System.currentTimeMillis());
PendingIntent i =PendingIntent.getActivity(getBaseContext(), 0,intent,0);
note.setLatestEventInfo(getBaseContext(),"Mensagens não lidas","Existem " + numMensagens + " mensagens não lidas",null);
// Hide the notification after its selected
note.flags |= Notification.FLAG_AUTO_CANCEL;
note.defaults |= Notification.DEFAULT_SOUND ;
notify.cancel(0x1);//retira se houver
notify.notify(0x1, note);
}
class Task implements Runnable
{
@Override
public void run()
{
//VERIFICAR AQUI SE HÁ UMA NOVA MENSAGEM
/*
int numMens = verificarMensagensNaoLidas();
if(numMens > 0)
showNotificationAlert(numMens);
*/
//executa de uma em uma hora
serviceHandler.postDelayed(this,3600000);// 1 hora
}
}
}
Consider also that if the user hangs up the phone, you should return your verification service, then also implement a Receiver class:
public class MessageVerifyServiceReceiver extends BroadcastReceiver {
private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context arg0, Intent arg1) {
if(arg1.getAction().equals(BOOT_COMPLETED_ACTION))
{
Intent myIntent = new Intent(arg0, MessageVerifyService.class);
arg0.startService(myIntent);
Toast.makeText(arg0, "Serviço verificador de mensagens iniciado novamente!",Toast.LENGTH_LONG).show();
}
}
}
You need to put this code in AndroidManifest.xml:
Permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Service declaration and receiver within Tag application:
<!-- SERVICES -->
<service
android:name=".MessageVerifyService"
android:exported="false"
android:process=":verifymessage" />
<!-- RECEIVER QUE INICIA JUNTO COM O DISPOSITIVO -->
<receiver
android:name=".MessageVerifyServiceReceiver"
android:exported="false"
android:label="@string/app_name"
android:process=":receiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>