Comandos PM de uso común de Android para iniciar el servicio y la transmisión

A menudo envío mensajes privados con comandos durante el desarrollo, pero no siempre puedo recordarlos
. Aquí hay algunos comandos verificados y se agregarán continuamente...

Servicio AMStart

Formatee
am start-foreground-service -n {nombre del paquete}/{nombre del paquete}.{Nombre del servicio}

// 不带数据
am start-foreground-service -n com.testsdk/.MainEntrance

// 有数据,接收方法:intent.getDataString();
am start-foreground-service -n com.testsdk/.MainEntrance -d 123

// 有数据,接收方法:intent.getStringExtra("index");
am start-foreground-service -n com.testsdk/.MainEntrance -e index hello

// 多数据,接收方法:
// intent.getStringExtra("index1");
// intent.getIntExtra("index2", -1);
am start-foreground-service -n com.testsdk/.MainEntrance --es index1 Str --ei index2 1234

Para versiones superiores de Android, al iniciar el Servicio, al usar startservice aparecerá el mensaje "la aplicación está en segundo plano uid nulo", por lo que debe reemplazarse con start-foreground-service.

Otros contenidos se actualizan continuamente. .

Durante la prueba, descubrí que el Servicio siempre se destruía después de unos segundos porque no había ningún servicio front-end configurado. Modifíquelo de la siguiente manera (contenido reimpreso):

private static final String ID = "channel_1";
private static final String NAME = "前台服务";
private void setForeground() {
    
    
	NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	NotificationChannel channel = new NotificationChannel(ID, NAME, NotificationManager.IMPORTANCE_HIGH);
	manager.createNotificationChannel(channel);
	Notification notification = new Notification.Builder(this, ID)
                .setContentTitle("收到一条重要通知")
                .setContentText("这是重要通知")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .build();
	startForeground(1, notification);
}

@Override
public void onCreate() {
    
    
	super.onCreate();
	setForeground();
}

Supongo que te gusta

Origin blog.csdn.net/cshoney/article/details/119949493
Recomendado
Clasificación