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.
TextView. You can addaddAction()toNotification.Builderand process thePendingIntent- tim_taller you need