0

I have a widget created for my app to launch. The data in the widget is getting updated after 30mins. I need to reduce the timing to around 2mins. I have heard that it can be done using AlarmService but I have no idea about how it should be implemented. I am sharing my widget code.

public class SampleWidget extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        RemoteViews view;
        SampleDatabase linksDatabase = new SampleDatabase(context);
        linksDatabase.openDb();
        List<MyMedia> myMedias = linksDatabase.getData();
        String date = null;
        String level = null;
        for (int j = 0; j < myMedias.size(); j++) {
            date = myMedias.get(j).getDateTime();
            level = myMedias.get(j).getLevel();
        }

        linksDatabase.closeDb();
        if (myMedias != null && myMedias.size() > 0) {

            for (int i = 0; i < myMedias.size(); i++) {
                final int n = appWidgetIds.length;
                for (int j = 0; j < n; j++) {
                    int appWidget = appWidgetIds[j];
                    Intent intnt = new Intent(context, MainActivity.class);
                    PendingIntent pi = PendingIntent.getActivity(context, 0,
                            intnt, 0);
                    view = new RemoteViews(context.getPackageName(),
                            R.layout.widget_layout);
                    view.setOnClickPendingIntent(R.id.relLayout, pi);
                    view.setTextViewText(R.id.date, date);
                    //view.setTextViewText(R.id.time, obtainedTime);
                    StringBuffer sb = new StringBuffer(level);
                    sb.append(" ");
                    sb.append("units");
                    view.setTextViewText(R.id.value_1, sb.toString());
                    appWidgetManager.updateAppWidget(appWidget, view);

                }

            }

        }
    }
}

Can anyone help me in finding a way for implementing. Thanks

Lavanya
  • 3,903
  • 6
  • 31
  • 57

1 Answers1

0

I got the solution for this and it is that you have to write a service where you will be updating the text and you will need to call the AlarmManager for every 2mins. In the service class you will be accessing the data by creating a database object and getting the details which updates the text in the widget.

Lavanya
  • 3,903
  • 6
  • 31
  • 57