Android不同系统版本依然能调用到正确的API方法Demo——Service调用startForeground举例

private static final Class<?>[] mSetForegroundSignature = new Class[] {
    boolean.class};
private static final Class<?>[] mStartForegroundSignature = new Class[] {
    int.class, Notification.class};
private static final Class<?>[] mStopForegroundSignature = new Class[] {
    boolean.class};

private NotificationManager mNM;
private Method mSetForeground;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mSetForegroundArgs = new Object[1];
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];

void invokeMethod(Method method, Object[] args) {
    try {
        method.invoke(this, args);
    } catch (InvocationTargetException e) {
        // Should not happen.
        Log.w("ApiDemos", "Unable to invoke method", e);
    } catch (IllegalAccessException e) {
        // Should not happen.
        Log.w("ApiDemos", "Unable to invoke method", e);
    }
}

/**
 * This is a wrapper around the new startForeground method, using the older
 * APIs if it is not available.
 */
void startForegroundCompat(int id, Notification notification) {
    // If we have the new startForeground API, then use it.
    if (mStartForeground != null) {
        mStartForegroundArgs[0] = Integer.valueOf(id);
        mStartForegroundArgs[1] = notification;
        invokeMethod(mStartForeground, mStartForegroundArgs);
        return;
    }

    // Fall back on the old API.
    mSetForegroundArgs[0] = Boolean.TRUE;
    invokeMethod(mSetForeground, mSetForegroundArgs);
    mNM.notify(id, notification);
}

/**
 * This is a wrapper around the new stopForeground method, using the older
 * APIs if it is not available.
 */
void stopForegroundCompat(int id) {
    // If we have the new stopForeground API, then use it.
    if (mStopForeground != null) {
        mStopForegroundArgs[0] = Boolean.TRUE;
        invokeMethod(mStopForeground, mStopForegroundArgs);
        return;
    }

    // Fall back on the old API.  Note to cancel BEFORE changing the
    // foreground state, since we could be killed at that point.
    mNM.cancel(id);
    mSetForegroundArgs[0] = Boolean.FALSE;
    invokeMethod(mSetForeground, mSetForegroundArgs);
}

@Override
public void onCreate() {
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    try {
        mStartForeground = getClass().getMethod("startForeground",
                mStartForegroundSignature);
        mStopForeground = getClass().getMethod("stopForeground",
                mStopForegroundSignature);
        return;
    } catch (NoSuchMethodException e) {
        // Running on an older platform.
        mStartForeground = mStopForeground = null;
    }
    try {
        mSetForeground = getClass().getMethod("setForeground",
                mSetForegroundSignature);
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException(
                "OS doesn't have Service.startForeground OR Service.setForeground!");
    }
}

@Override
public void onDestroy() {
    // Make sure our notification is gone.
    stopForegroundCompat(R.string.foreground_service_started);
}
摘自:http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 uni-app 中,可以通过 uni.requireNativePlugin 方法调用 native.js 中的方法。具体步骤如下: 1. 在 uni-app 项目中创建 Native 模块,用于调用 native.js 中的方法: ```javascript const nativeModule = uni.requireNativePlugin("NativeModule"); ``` 2. 在 Native 模块中定义调用 startForeground 方法的函数: ```javascript function startForeground() { nativeModule.startForeground(); } ``` 3. 在 native.js 中实现 startForeground 方法,同样需要导入相关的 Android 类: ```javascript const Context = require("@android/context"); const Notification = require("@android/app/Notification"); const NotificationManagerCompat = require("@android/app/NotificationManagerCompat"); const NotificationChannel = require("@android/app/NotificationChannel"); function startForeground() { const service = getContext().getService(); const channel = new NotificationChannel("channel_id", "channel_name", NotificationManagerCompat.IMPORTANCE_HIGH); const notificationManager = NotificationManagerCompat.from(Context); const notification = new Notification.Builder(Context, "channel_id") .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle("Foreground Service") .setContentText("Service is running in foreground") .build(); service.startForeground(1, notification); } ``` 4. 在 Native 模块中调用 startForeground 方法: ```javascript function startForeground() { nativeModule.startForeground(); } ``` 这样就可以通过 uni-app 调用 native.js 中的 startForeground 方法开启前台服务了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值