android widget 启动activity,在Android手机启动时启动Activity
时间: 2024-03-01 12:55:51 浏览: 32
要在Android手机启动时启动Activity,可以通过创建一个BroadcastReceiver来实现。具体步骤如下:
1. 创建一个BroadcastReceiver类,重写onReceive方法。
2. 在onReceive方法中,使用Intent启动目标Activity。
3. 在AndroidManifest.xml文件中注册BroadcastReceiver,并添加BOOT_COMPLETED权限。
下面是一个示例代码:
```java
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, TargetActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
```
在AndroidManifest.xml文件中注册BroadcastReceiver:
```xml
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
```
同时需要添加BOOT_COMPLETED权限:
```xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
```
这样,在Android手机启动时,就会启动目标Activity了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)