There is a foreground service that starts when the application is started in onResume () MainActivity

@Override protected void onResume() { super.onResume(); mSensorManager.registerListener(PedometerActivity.this, mSensor, SensorManager.SENSOR_DELAY_FASTEST); mPedometerService = new Intent(PedometerActivity.this, PedometerService.class); startService(mPedometerService); } 

The custom service itself:

 public class PedometerService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Intent notificationIntent = new Intent(this, PedometerActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pendingIntent); Notification notification; notification = builder.build(); notification.contentView = new RemoteViews(getPackageName(), R.layout.pedometer_service); startForeground(777, notification); return Service.START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } } 

and its display:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/azure" android:orientation="horizontal"> <ImageView android:id="@+id/imagenotileft" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="8dp" android:layout_alignParentLeft="true" android:layout_centerInParent="true" android:contentDescription="@string/app_name" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:layout_centerInParent="true" android:layout_toRightOf="@+id/imagenotileft" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dp" android:text="@string/today_distance"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textStyle="bold" android:text="13250m"/> </LinearLayout> <ImageView android:id="@+id/state" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="8dp" android:layout_centerInParent="true" android:layout_alignParentRight="true" android:contentDescription="@string/app_name" android:src="@mipmap/green_foot"/> </RelativeLayout> 

How to do this (I suppose that somehow you need to bind the intent), so that by clicking on the right ImageView (state) you can get control and perform some actions? Now when you click on the service, the application is launched, but it is necessary that when you click on the ImageView, the action is performed, and if clicked in a different place, then activate it.

  • it is impossible to click separately on the picture and separately on TextView . You can add addAction() to Notification.Builder and process the PendingIntent - tim_taller you need

1 answer 1

I need to get RemoteViews for the Notification service and assign a broadcast listener to my ImageView. The problem was that the listener also needed to be announced in the manifesto, and I, as a beginner, did not know about it. Therefore, the code did not work properly.

In the onStartCommand () service add:

 RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.pedometer_service); Intent buttonStateIntent = new Intent(this, NotificationStateButtonHandler.class); buttonStateIntent.putExtra("action", "close"); PendingIntent buttonClosePendingIntent = pendingIntent.getBroadcast(this, 0, buttonStateIntent, 0); remoteViews.setOnClickPendingIntent(R.id.state, buttonClosePendingIntent); notification.contentView = remoteViews; 

Next, in the broadcast listener, we change the state of the picture and the entire program as a whole, sending an service to the service with the necessary data:

 public class NotificationStateButtonHandler extends BroadcastReceiver { private Intent mPedometerService; @Override public void onReceive(Context context, Intent intent) { // Change pedometer state (on to off, or off to on) if (PedometerService.isListenAccelerometer() == false) { SendIntentToService(context, PedometerService.ACCELEROMETER_ON); } else { SendIntentToService(context, PedometerService.ACCELEROMETER_OFF); } } // Create new intent and send it to service for transmitting in service new value of // accelerometer listener (on or off) private void SendIntentToService(Context context, int lister_state) { mPedometerService = new Intent(context, PedometerService.class); mPedometerService.putExtra(PedometerService.ACCELEROMETER_KEY, lister_state); context.startService(mPedometerService); } } 

Well, do not forget to announce the whole thing in the manifesto:

  <receiver android:name="NotificationStateButtonHandler" > </receiver>