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 turned off, you can notify him that there is a new message.
Be aware that as your application performs the query in an external database, maybe the check interval should be equal to or more than one hour.
Service example:
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
}
}
}
Also think that if the user shuts down 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 on Androidmanifest.xml:
Permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Declaration of service and receiver within the 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>
I don’t see any other way to do it, I think you really need to have a threar running.
– Jorge B.
I don’t know if that’s what you want, but take a look at http://developer.android.com/google/gcm/index.html
– user3174
That’s right Paul, thank you
– Gabriel Duarte