-2

I am new with AppWidgetProvider and service class in android.Here i'm trying to do 2 things:

1 - get click listner of widget in which i want to call isservicerunning() method which is defined below.

2-in isservicerunning() method if returns true i want to call one static method declared in MyService(which is running) class.

I am not getting how to do this? Is there any way then please send here.Help will be appreciated.Thank you.

code is given below:

MyWidget.java:

    public class MyWidget extends AppWidgetProvider {

    static Context cont;
        static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                    int appWidgetId) {
            cont=context;
            Intent intent = new Intent(context, MyService.class);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
            views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }

        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            // There may be multiple widgets active, so update all of them

            for (int appWidgetId : appWidgetIds) {
                updateAppWidget(context, appWidgetManager, appWidgetId);
            }
        }

        private static boolean isServiceRunning() {
            ActivityManager manager = (ActivityManager) cont.getSystemService(ACTIVITY_SERVICE);
            for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
                if ("MY_PACKAGE_NAME.MyService"
                        .equals(service.service.getClassName())) {
//here i want to call running MyService class's one static method                
                 return true;
                }
            }
            return false;
        }

        @Override
        public void onEnabled(Context context) {
            // Enter relevant functionality for when the first widget is created

        }

        @Override
        public void onDisabled(Context context) {
            // Enter relevant functionality for when the last widget is disabled
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
        }
    }

Here MyService is class which extends Service. I want to call one static method of it as described above.

user8209191
  • 37
  • 1
  • 7

1 Answers1

0

Use broadcast receivers to communicate between two android components

http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

Satender Kumar
  • 627
  • 4
  • 7
  • i have tried but it only calls first time: Intent intent2 = new Intent(); intent2.setAction("com.example.admin.testingproject"); cont.sendBroadcast(intent2); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent2, 0); Log.e("zxczxc", "updateAppWidget"); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget); views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent); – user8209191 Apr 25 '18 at 09:03