美文网首页
Android前台Service的一个骚操作

Android前台Service的一个骚操作

作者: czs_0_0_0_0 | 来源:发表于2018-08-03 22:37 被阅读0次

最近在学习Android camera2相关的API,突然发现如果将camera相关的api与前台service相结合的话岂不是可以实现偷拍的效果。大体的思路是启动app后再启动一个前台service。之后把当前的Activity退到后台,通过前台Service的自定义通知栏接收用户的点击事件后回调发送广播给Activity来进行拍照相关的各种操作。

不过这类的东西终非正道,不打算做深入研究,贴上其中一段demo做个标记。

以下为前台Service的代码

package jp.camera.gentleman;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

public class GentlemanService extends Service {
    private static final String TAG = "GentlemanService";

    public static final String STR_FINISH = "finish";

    // 切换前后摄像头
    public static final String ACTION_CHANGE_CAMERA = "jp.camera.gentleman.action_change_camera";
    // 拍照
    public static final String ACTION_CAPTURE = "jp.camera.gentleman.action_capture";
    // 杀掉service
    public static final String ACTION_FINISH = "jp.camera.gentleman.action_finish";

    private static final String CHANNEL = "gentleman";

    public static boolean isRunning = false;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_FINISH);
        registerReceiver(mBroadcastReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getBooleanExtra(STR_FINISH, false)) {
            isRunning = false;
            stopForeground(true);
            stopSelf();
        } else {
            isRunning = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(CHANNEL, getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(getColor(R.color.colorPrimary));
                notificationChannel.enableVibration(false);
                NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                if (notificationManager != null) {
                    notificationManager.createNotificationChannel(notificationChannel);
                }
            }

            RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.item_notification);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_CHANGE_CAMERA), 0);
            remoteViews.setOnClickPendingIntent(R.id.change_camera, pendingIntent);

            pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_CAPTURE), 0);
            remoteViews.setOnClickPendingIntent(R.id.capture, pendingIntent);

            pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_FINISH), 0);
            remoteViews.setOnClickPendingIntent(R.id.finish, pendingIntent);

            Notification notification = new NotificationCompat.Builder(this, CHANNEL)
                    .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                    .setContent(remoteViews)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();

            startForeground(1, notification);
        }

        return super.onStartCommand(intent, flags, startId);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            stopSelf();
        }
    };
}

以下为通知栏通知的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/change_camera"
        android:text="Fate"
        android:textColor="@color/colorPrimary"
        android:textSize="16sp"
        android:padding="8dp"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/capture"
        android:text="Stay"
        android:textColor="@color/colorPrimary"
        android:textSize="16sp"
        android:padding="8dp"
        android:layout_centerHorizontal="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/finish"
        android:text="Night"
        android:textColor="@color/colorPrimary"
        android:textSize="16sp"
        android:padding="8dp"
        android:layout_alignParentEnd="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

Activity的代码就不贴了,懂的人自然懂。

相关文章

网友评论

      本文标题:Android前台Service的一个骚操作

      本文链接:https://www.haomeiwen.com/subject/jntpvftx.html