Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当设备打开或解锁时启动应用程序_Android_Events - Fatal编程技术网

Android 当设备打开或解锁时启动应用程序

Android 当设备打开或解锁时启动应用程序,android,events,Android,Events,1.当设备刚刚打开时,如何启动/打开应用程序 2.当设备刚刚解锁/退出锁定状态时,如何启动/打开应用程序 (我的意思是,如何识别这些事件?) 10q 大卫 这是我的新代码: 在舱单中: <receiver android-permission="android.permission.RECEIVE_BOOT_COMPLETED" android:name="BootReciver" > <intent-filter > <action and

1.当设备刚刚打开时,如何启动/打开应用程序

2.当设备刚刚解锁/退出锁定状态时,如何启动/打开应用程序

(我的意思是,如何识别这些事件?)

10q

大卫

这是我的新代码:

在舱单中:

<receiver
  android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
  android:name="BootReciver" >
  <intent-filter >
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      <action android:name="android.intent.action.SCREEN_ON" />
  </intent-filter>
</receiver>
爷爷课是我的主要活动

此外,我直接在我的设备上运行代码,这就是我在LogCat中得到的:

??:??:信息/():无法打开日志设备“/dev/log/main”:没有这样的文件或目录

我还尝试为boot添加一个接收器,为ScreenOn添加另一个接收器(有两个类),如下所示:

<receiver
android:name="ScreenOnReciver" >
    <intent-filter >
    <action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>

<receiver
android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:name="BootReciver" >
<intent-filter >
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
 </receiver>
编辑2:

我在日志中只有以下“例外情况”:

11-14 00:22:15.313: WARN/WindowManager(244): android.view.InflateException: Binary XML  file line #7: Error inflating class <unknown>
11-14 00:22:15.313: WARN/WindowManager(244): Caused by: java.lang.reflect.InvocationTargetException
11-14 00:22:15.313: WARN/WindowManager(244): Caused by:   android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or   path): TypedValue{t=0x2/d=0x1010059 a=-1}
11-14 00:22:15.313:WARN/WindowManager(244):android.view.InflateException:二进制XML文件行#7:膨胀类时出错
11-14 00:22:15.313:WARN/WindowManager(244):原因:java.lang.reflect.InvocationTargetException
11-14 00:22:15.313:WARN/WindowManager(244):原因:android.content.res.Resources$NotFoundException:资源不是可绘制的(颜色或路径):TypedValue{t=0x2/d=0x1010059 a=-1}

您要做的是设置一个BroadcastReceiver来监听这些操作。更多信息:

对于你的情况,你会有一个接受者

public class YourReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intent = new Intent(context, YourActivity.class);
        context.startActivity(intent);
    }
}
并在您的舱单中注册:

    <receiver
        android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:name="YourReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>


Hi Craigy,答案和链接是10倍。还有一个问题,我必须把上面的代码放在哪里?在一个单独的项目中?还是在同一个项目中?它将在同一个项目中。您将为接收方创建一个新的Java类,并将
标记添加到
标记内的AndroidManifest.xml文件中。例如:我有一个名为“a”的应用程序项目。在这个项目中,我想添加一个代码,每当设备打开或解锁应用程序(“a”)时,它都会打开。可以使用上述概念完成吗?您好,“startActivity(intent)”无法识别,如何修复?很好的调用,应该是
context。startActivity(intent)
我不知道为什么会出现日志错误,但我相信这与这个问题无关。搜索解决方案或发布新问题。我根据以下说明修复此问题:该日志不是堆栈跟踪。寻找写着“Exception at…”的行嗨Craigy,有没有可能通过私人邮件向您索要一个工作示例代码?没有,因为我没有一个工作示例,其他人将无法从解决您的问题的过程中获益。您当前面临的错误在您发布的logcat异常中详细说明。您正在使用一个XML文件的第7行中不可绘制的资源。
public class YourReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intent = new Intent(context, YourActivity.class);
        context.startActivity(intent);
    }
}
    <receiver
        android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:name="YourReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>