无法启用探头/传感器

发布于 2024-12-09 08:14:04 字数 3535 浏览 0 评论 0原文

我正在使用 funf 框架来访问 SMS在用户的手机上。

该框架由几个包组成。提供的探针都扩展了抽象类 Probe。

为了利用该框架,我编写了以下类:

package com.senseapp.dieselboris;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class SenseAppV1Activity extends Activity {
    private long p;
    private String message;
    private boolean r; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SMSmessage sp = new SMSmessage();
        r = sp.isRunning();
        sp.enable();
        p = sp.getDefaultPeriod();

        message = Boolean.toString(r);

        TextView tv = new TextView(this);
        tv.setText(message);
        setContentView(tv);

    }
}

因为 SMSProbe 类的方法被声明为受保护,所以我编写了 SMSmessage 类:

package com.senseapp.dieselboris;

import edu.mit.media.funf.probe.*;
import edu.mit.media.funf.probe.builtin.*;
import edu.mit.media.funf.probe.builtin.ProbeKeys.AndroidInternal.Sms;

public class SMSmessage extends SMSProbe {

    protected String getDataName () {
        return super.getDataName();

    }

    protected long getDefaultPeriod () {
        return super.getDefaultPeriod();
    } 

    protected String getDateColumnName () {
        return super.getDateColumnName();
    }


}

但是当我运行此代码时,它在打开探测器时失败,请参阅此错误:

<small>
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.senseapp.dieselboris/com.senseapp.dieselboris.SenseAppV1Activity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:144)
     at android.app.ActivityThread.main(ActivityThread.java:4937)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
     at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:271)
     at edu.mit.media.funf.probe.Probe.sendProbeStatus(Probe.java:280)
     at edu.mit.media.funf.probe.Probe.sendProbeStatus(Probe.java:227)
     at edu.mit.media.funf.probe.Probe.enable(Probe.java:612)
     at com.senseapp.dieselboris.SenseAppV1Activity.onCreate(SenseAppV1Activity.java:20)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)


</small>

抽象类 Probe 中的方法enable():

public final void enable() {
        if (!enabled) {
            Log.i(TAG, "Enabling probe: " + getClass().getName());
            enabled = true;
            running = false;
            sendProbeStatus();
            onEnable();
        }
    }

我做错了什么?由于我的类 SMSmessage 最终继承自 Probe,我认为这是有效的。

我希望 Funf 的开发者之一能读到这篇文章。

谢谢。

I am using the funf framework in order to get access to the SMS's on the users' phone.

The framework consists of several packages. The provided probes all extend the abstract class Probe.

In order to utilize the framework i have wrote the following class:

package com.senseapp.dieselboris;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class SenseAppV1Activity extends Activity {
    private long p;
    private String message;
    private boolean r; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SMSmessage sp = new SMSmessage();
        r = sp.isRunning();
        sp.enable();
        p = sp.getDefaultPeriod();

        message = Boolean.toString(r);

        TextView tv = new TextView(this);
        tv.setText(message);
        setContentView(tv);

    }
}

Because the methods of the the class SMSProbe are declared protected i wrote the SMSmessage class:

package com.senseapp.dieselboris;

import edu.mit.media.funf.probe.*;
import edu.mit.media.funf.probe.builtin.*;
import edu.mit.media.funf.probe.builtin.ProbeKeys.AndroidInternal.Sms;

public class SMSmessage extends SMSProbe {

    protected String getDataName () {
        return super.getDataName();

    }

    protected long getDefaultPeriod () {
        return super.getDefaultPeriod();
    } 

    protected String getDateColumnName () {
        return super.getDateColumnName();
    }


}

but when i run this code it fails when it should turn on the probe, see this error:

<small>
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.senseapp.dieselboris/com.senseapp.dieselboris.SenseAppV1Activity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:144)
     at android.app.ActivityThread.main(ActivityThread.java:4937)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
     at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:271)
     at edu.mit.media.funf.probe.Probe.sendProbeStatus(Probe.java:280)
     at edu.mit.media.funf.probe.Probe.sendProbeStatus(Probe.java:227)
     at edu.mit.media.funf.probe.Probe.enable(Probe.java:612)
     at com.senseapp.dieselboris.SenseAppV1Activity.onCreate(SenseAppV1Activity.java:20)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)


</small>

the method enable() in the abstract class Probe:

public final void enable() {
        if (!enabled) {
            Log.i(TAG, "Enabling probe: " + getClass().getName());
            enabled = true;
            running = false;
            sendProbeStatus();
            onEnable();
        }
    }

What do i do wrong? since my class SMSmessage eventually inherits from Probe this is valid I thought.

I hope one of the developers of Funf reads this.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-12-16 08:14:04

在 Funf 中,探针就是服务。这意味着它们不能直接实例化,而必须由系统使用意图创建。以下是如何使用意图从 Probe 服务请求数据的示例。 (下面的示例适用于 v0.3.x 及更高版本。)

探测器使用异步方法发送状态和数据,因此您将需要 Service 或 BroadcastReceiver 来接收探测器信息。

首先使用服务创建回调意图:

Intent callbackIntent = new Intent(this, ExampleProbeDataService.class); 
callbackIntent.setAction("CUSTOM_DATA_ACTION");
PendingIntent callback = PendingIntent.getService(getContext(), 0, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);

或使用广播:

Intent callbackIntent = new Intent("CUSTOM_BROADCAST_ACTION"); 
// Restrict broadcast to only my package to prevent broadcasting private user data
callbackIntent.setPackage(getContext().getPackageName()); 
PendingIntent callback = PendingIntent.getBroadcast(getContext(), 0, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);

然后向探测器发出数据请求:

Bundle params = new Bundle();
params.putLong(Parameter.Builtin.PERIOD.name, 10L); // Run every 10 seconds

Intent probeIntent = new Intent(getContext(), probeClass);
probeIntent.setAction(Probe.ACTION_REQUEST);
probeIntent.putExtra(Probe.CALLBACK_KEY, callback);
probeIntent.putExtra(Probe.REQUESTS_KEY, params);
getContext().startService(probeIntent);

探测器将根据您提供的参数定期发送数据和状态消息。

虽然在某些情况下需要直接从探测器请求数据,但对于大多数情况,这不是推荐的方法。对于测试,我建议使用 ProbeTestCase 类。它负责请求数据和处理响应的细节。对于应用程序,最简单的方法是扩展 ConfiguredPipeline,并重写 onDataReceived(Bundle data) 以自定义接收数据的处理。

In Funf, probes are Services. This means they cannot be directly instantiated, but instead have to be created by the system using intents. Here is an example of how to request data from a Probe service using intents. (The example below is for v0.3.x and above.)

Probes use asynchronous methods for sending status and data, so you will need a Service or BroadcastReceiver to receive probe information.

First create your callback intent using either a Service:

Intent callbackIntent = new Intent(this, ExampleProbeDataService.class); 
callbackIntent.setAction("CUSTOM_DATA_ACTION");
PendingIntent callback = PendingIntent.getService(getContext(), 0, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);

or using a Broadcast:

Intent callbackIntent = new Intent("CUSTOM_BROADCAST_ACTION"); 
// Restrict broadcast to only my package to prevent broadcasting private user data
callbackIntent.setPackage(getContext().getPackageName()); 
PendingIntent callback = PendingIntent.getBroadcast(getContext(), 0, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Then make your data request to the probe:

Bundle params = new Bundle();
params.putLong(Parameter.Builtin.PERIOD.name, 10L); // Run every 10 seconds

Intent probeIntent = new Intent(getContext(), probeClass);
probeIntent.setAction(Probe.ACTION_REQUEST);
probeIntent.putExtra(Probe.CALLBACK_KEY, callback);
probeIntent.putExtra(Probe.REQUESTS_KEY, params);
getContext().startService(probeIntent);

The probe will periodically send data and status messages based on the parameters you provide.

While requesting data from probes directly is required in some cases, it is not the recommended approach for most cases. For testing, I recommend using the ProbeTestCase class. It takes care of the details of requesting data and handling the responses. For an app, the easiest approach is to extend ConfiguredPipeline, and override onDataReceived(Bundle data) to customize the handling of data that is received.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文