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>
在BootReciver.java中,我添加到了我的项目中:
package development.Grandpa;
import android.content.BroadcastReceiver;
public class BootReciver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, Grandpa.class);
context.startActivity(intent1);
}
}
爷爷班是我的主要活动。
此外,我正在我的设备上直接运行代码,这就是我在LogCat中获得的内容:
?? - ?? ??:??:??。???:INFO /():无法打开日志设备'/ dev / log / main':没有这样的文件或目录
我还尝试添加一个reciver用于启动,另一个用于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>
但它也没有帮助。
编辑:
这是我在logcat之后得到的:
1.运行应用程序
2.退出申请
3.锁定屏幕
4.解锁屏幕
11-13 23:59:26.140: ERROR/AndroidRuntime(3640): Set to default setting_6 : region=- Duser.region=IL propRegn=IL
11-13 23:59:26.710: ERROR/AndroidRuntime(3649): Set to default setting_6 : region=-Duser.region=IL propRegn=IL
11-13 23:59:33.246: ERROR/power(244): *** set_screen_state 0
11-13 23:59:33.246: ERROR/SensorManager(244): unregisterListener:: all sensors, listener = com.android.server.PowerManagerService$13@40517118
11-13 23:59:33.593: ERROR/LockPatternKeyguardView(244): Phone is boot completed. so can send broadcast message for music player
11-13 23:59:35.286: ERROR/power(244): *** set_screen_state 1
11-13 23:59:35.286: ERROR/SensorManager(244): registerListener :: handle = 3 name= GP2A Light Sensor delay= 200000 Listener= com.android.server.PowerManagerService$13@40517118
11-13 23:59:35.286: ERROR/SensorManager(244): reg :: handle = 3
11-13 23:59:35.336: ERROR/LockPatternKeyguardView(244): Phone is boot completed. so can send broadcast message for music player
11-13 23:59:36.360: ERROR/SlidingTab(244): onTouchEvent() : thresholdReached !
11-13 23:59:36.380: ERROR/KeyguardViewMediator(244): Phone is boot completed. so can broadcast
11-13 23:59:36.400: ERROR/MTPRx(3319): In MtpReceiverandroid.intent.action.USER_PRESENT
11-13 23:59:36.410: ERROR/MTPRX(3319): Battery charging. plugType = 2
11-13 23:59:36.410: ERROR/MTPRx(3319): USB charging
11-13 23:59:36.416: ERROR/MTPRx(3319): usb is connected, set value in Settings.System, result = true
11-13 23:59:36.416: ERROR/MTPRx(3319): usb mode = 0
11-13 23:59:36.416: ERROR/MTPRx(3319): usb debugging is enabled
Edit2:
我在logcat中只有这些“异常”:
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}
答案 0 :(得分:7)
您要做的是设置一个侦听这些操作的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>