Create notifications service

This commit is contained in:
Pierre 2018-04-09 10:22:47 +02:00
parent 32eed6322f
commit 939f24937a
3 changed files with 81 additions and 0 deletions

View File

@ -34,6 +34,10 @@
android:name=".ui.activities.SearchUserActivity"
android:label="@string/activity_searchuser_title"/>
<!-- Notifications background refresh service -->
<service android:name=".data.services.NotificationsService"
android:exported="false"/>
</application>
</manifest>

View File

@ -0,0 +1,72 @@
package org.communiquons.android.comunic.client.data.services;
import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Notifications service
*
* @author Pierre HUBERT
* Created by pierre on 4/9/18.
*/
public class NotificationsService extends IntentService {
/**
* Debug tag
*/
private static final String TAG = "NotificationsService";
/**
* Keep run status
*/
private boolean run;
/**
* Public constructor
*/
public NotificationsService(){
super("NotificationsService");
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Log.v(TAG, "Start service");
while(run){
try {
//Make a pause
Thread.sleep(2000);
} catch (InterruptedException e){
Thread.currentThread().interrupt();
}
Log.v(TAG, "Hey there, service !");
}
Log.v(TAG, "Stop service");
}
@Override
public void onCreate() {
super.onCreate();
this.run = true;
}
@Override
public void onDestroy() {
super.onDestroy();
this.run = false;
}
}

View File

@ -24,6 +24,7 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper;
import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper;
import org.communiquons.android.comunic.client.data.friendsList.FriendRefreshLoopRunnable;
import org.communiquons.android.comunic.client.data.services.NotificationsService;
import org.communiquons.android.comunic.client.data.utils.UiUtils;
import org.communiquons.android.comunic.client.ui.fragments.ConversationFragment;
import org.communiquons.android.comunic.client.ui.fragments.ConversationsListFragment;
@ -132,6 +133,10 @@ public class MainActivity extends AppCompatActivity
friendsListRefreshThread = new Thread(
new FriendRefreshLoopRunnable(getApplicationContext(), dbHelper));
friendsListRefreshThread.start();
//Start notification thread
Intent intent = new Intent(this, NotificationsService.class);
startService(intent);
}
@Override