I just started to learn Android and I wrote the following code followed a tutorial.
This is updating the textarea if click on it, but I would like to update in each 30 seconds. How can I do that?
I would like to measure my pulse and sending the data through Bluetooth. If my pulse is to high than the widget is warning me(Sound). What is the best way to manage this, I would like to check this in each 15 seconds. What is the most efficient way?
package com.test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import com.test.R;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class WidgetActivity extends AppWidgetProvider {
public static WidgetActivity Widgetke = null;
public static Context context;
public static AppWidgetManager appWidgetManager;
public static int appWidgetIds[];
@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ) {
if (null == context) context = WidgetActivity.context;
if (null == appWidgetManager) appWidgetManager = WidgetActivity.appWidgetManager;
if (null == appWidgetIds) appWidgetIds = WidgetActivity.appWidgetIds;
WidgetActivity.Widgetke = this;
WidgetActivity.context = context;
WidgetActivity.appWidgetManager = appWidgetManager;
WidgetActivity.appWidgetIds = appWidgetIds;
Log.i("PXR", "onUpdate");
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
updateAppWidget(context,appWidgetManager, appWidgetId);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
DateFormat format = SimpleDateFormat.getTimeInstance( SimpleDateFormat.MEDIUM, Locale.getDefault() );
CharSequence text = "Time: " + format.format( new Date());
Intent intent = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setOnClickPendingIntent(R.id.mainlayout, pendingIntent);
remoteViews.setTextViewText(R.id.test_out, text);
// Tell the widget manager
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
WidgetActivity.Widgetke.onUpdate(context, appWidgetManager, appWidgetIds);
Toast.makeText(context, "Update Widget", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
}
You can start up a new thread and use a for(;;) loop with a sleep. You can update the UI using a Handler (android.os.Handler) from there: http://www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/
Alternatively, use a timer.