Android はサービスとブロードキャストを開始するために PM コマンドをよく使用します

開発中にコマンドを頻繁にPMするのですが、いつも覚えられないので
、検証済みのコマンドをいくつか紹介します。今後も追加していく予定です...

AMスタートサービス

形式
am start-foreground-service -n {パッケージ名}/{パッケージ名}.{サービス名}

// 不带数据
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

Android の上位バージョンでは、サービスを開始するときに startservice を使用すると「アプリはバックグラウンド uid null」というメッセージが表示されるため、これを start-foreground-service に置き換える必要があります。

その他のコンテンツも継続的に更新中です。

テスト中に、フロントエンド サービスが設定されていないため、サービスは常に数秒後に破棄されることがわかりました。次のように変更します (内容の再掲)。

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();
}

おすすめ

転載: blog.csdn.net/cshoney/article/details/119949493
おすすめ