I want to create a service which will update a Textview continuously, even if I run an another app. The service will be stopped if I push the stop button. In the following code, I use the combination of the service and the BroadcastReceiver, but the service doesn't update the TextView if I change the application.
Code:
Service
public class ChronometerService extends Service
{
public static final String BROADCAST_ACTION = "com.example.test";
private final Handler handler = new Handler();
private Intent intent = null;
int counter = 0;
@Override
public void onCreate()
{
super.onCreate();
intent = new Intent( BROADCAST_ACTION );
}
@Override
public void onStart( Intent intent, int startId )
{
//handler.removeCallbacks( sendUpdatesToUI );
handler.postDelayed( sendUpdatesToUI, 1000 );
}
@Override
public IBinder onBind( Intent intent )
{
return null;
}
private Runnable sendUpdatesToUI = new Runnable()
{
@Override
public void run()
{
DisplayLoggingInfo();
handler.postDelayed( this, 1000 );
}
};
private void DisplayLoggingInfo()
{
counter += 1;
//chrono.start();
intent.putExtra( "TIME", String.valueOf(counter) );
sendBroadcast( intent );
}
@Override
public void onDestroy()
{
//chrono.stop();
//chrono.setBase( SystemClock.elapsedRealtime() );
handler.removeCallbacks( sendUpdatesToUI );
super.onDestroy();
}
}
Activity
public class MainActivity extends Activity
{
public static TextView chronometer1 = null;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent( MainActivity.this, ChronometerService.class );
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive( Context context, Intent intent )
{
updateUI( intent );
}
};
private void updateUI( Intent intent )
{
String time = intent.getStringExtra( "TIME" );
chronometer1 = ( TextView ) findViewById( R.id.chronometer1 );
chronometer1.setText( time );
}
public void start( View v )
{
startService( intent );
registerReceiver(broadcastReceiver, new IntentFilter( ChronometerService.BROADCAST_ACTION ) );
}
public void stop( View v )
{
unregisterReceiver( broadcastReceiver );
stopService( intent );
}
}
I found the answer: When the service is running and I do not want it to be killed by the OS, I have to use the startForeground(int id, Notification notification) method.
When the service finishes whatever is doing and can be killed by the OS, call stopForeground(boolean removeNotification). "startForeground" requires a notification as argument because every foreground service must display a notification so the user realizes about it.
Also helps to set in the service declaration in the manifest file the attribute:
android:process=":some name here"