`
ylzyd12345
  • 浏览: 120313 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
博客专栏
713488f4-d0a9-3f35-997f-199fb0cb3563
玩懂Log,打开Andro...
浏览量:10355
社区版块
存档分类
最新评论

开机后自动启动Activity中需要注意的

阅读更多

 

public class StartupReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		try {
			System.out.println("接受到广播");
//			Intent _intent = new Intent(context,OtherActivity.class);
//			_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			Intent _intent = new Intent();
			_intent.setAction("com.michael.TEST");
			_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(_intent);
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

 

 

注:context.startActivity(_intent); 需要加上Flag:_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

 

 一种是显式 new Intent(context,OtherActivity.class);

 注:一种是隐式_intent.setAction("com.michael.TEST");

 隐式的intent-filter 需要加上 <category android:name="android.intent.category.DEFAULT" />

 

 

 <activity android:name=".OtherActivity" >
            <intent-filter >
                <action android:name="com.michael.TEST" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
 </activity>

最容易忽略的就是: 测试代码是否正确。 要先运行程序在虚拟机。 然后重启系统!!!才能验证结果。第一次运行是不会验证的。

 

 

 写道
下面是为什么需要设置:android.intent.category.DEFAULT
In principle, therefore, an Intent object with no categories should always pass this test, regardless of what's in the filter. That's mostly true. However, with one exception, Android treats all implicit intents passed to startActivity() as if they contained at least one category: "android.intent.category.DEFAULT" (the CATEGORY_DEFAULT constant). Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters. (Filters with "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" settings are the exception. They mark activities that begin new tasks and that are represented on the launcher screen. They can include "android.intent.category.DEFAULT" in the list of categories, but don't need to.) See Using intent matching, later, for more on these filters.)
下面是为什么要设置:FLAG_ACTIVITY_NEW_TASK
Note that if this method is being called from outside of an Activity Context, then the Intent must include the FLAG_ACTIVITY_NEW_TASK launch flag. This is because, without being started from an existing Activity, there is no existing task in which to place the new activity and thus it needs to be placed in its own separate task.
1
2
分享到:
评论
1 楼 silentJesse 2012-03-16  
good good

相关推荐

    安卓开机自动启动应用程序

    本教程将详细讲解如何实现安卓开机自动启动应用程序,以及涉及到的相关知识点。 首先,我们需要理解安卓系统的启动流程。在安卓设备开机时,系统会启动一个名为"SystemServer"的进程,它负责初始化系统服务和启动...

    Android应用开机自动启动

    开机自动启动的关键在于使用`BroadcastReceiver`来监听系统的`ACTION_BOOT_COMPLETED`广播,这个广播在系统完成启动后发送。首先,我们需要创建一个`BroadcastReceiver`子类,例如命名为`BootReceiver`: ```java ...

    安卓开发实现开机启动跳转到指定activity

    在应用的启动Activity中添加以下代码: ```java if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) { if (getSelfPermission(Manifest.permission.RECEIVE_BOOT_COMPLETED) != PackageManager.PERMISSION_...

    android应用开机自启动

    本知识点主要讲解如何利用Android的广播接收器(BroadcastReceiver)来实现在手机开机时自动启动应用。 一、开机自启动原理 Android系统在启动完成后会发送一个名为ACTION_BOOT_COMPLETED的广播,任何注册了这个...

    Android应用开机时自动启动

    3. **启动服务或Activity**:在`onReceive()`方法中,你可以启动你的服务或主Activity,以达到应用自动启动的效果。例如,如果你有一个服务名为MyService,可以这样启动: ```java Intent serviceIntent = new ...

    Android后台保持运行,开机后自动启动设定好的APK的DEMO.zip

    开机自动启动应用的关键在于注册一个BroadcastReceiver,监听系统的`ACTION_BOOT_COMPLETED`广播事件。当设备开机完成后,系统会发送这个广播,你的BroadcastReceiver接收到这个广播后,可以启动你的服务或者指定的...

    【Android】开机自启动2

    在Android系统中,"开机自启动"是指当设备完成启动过程后,某些应用程序或服务能够自动启动并开始运行。这通常涉及到系统级别的设置和权限管理,对于开发者来说,理解和掌握这个功能是创建后台服务和持续运行应用的...

    android动态设置开机自动启动程序

    在Android系统中,实现动态设置开机自动启动程序是一项常见的需求,尤其对于开发者和系统优化者来说,这有助于确保某些服务或应用在设备启动时能够自动运行。本文将深入探讨如何在Android平台上实现实现这一功能。 ...

    Android 开机自动启动一个程序

    在Android系统中,实现开机自动启动一个程序,通常涉及到服务(Service)、BroadcastReceiver(广播接收器)以及AlarmManager等组件的使用。以下将详细介绍如何在Android应用中设置开机启动一个Activity并发送通知。 1....

    Android中实现开机自动启动服务(service)实例

    还是做成一个 apk 吧,暂定只写一个 service 并开机自动启用,无 activity 的。 Java 中调用 native 程序我选择使用 JNI 方式,直接在 JNI_OnLoad 方法中调用 pthread_create 创建个线程跑原来的 main 就行啦。 ...

    android 开机自启动+后台服务+自动关机

    要实现应用在开机时自动启动,你需要在AndroidManifest.xml文件中声明一个BroadcastReceiver,并设置对应的权限: ```xml ``` 然后,在BootReceiver类中,接收到ACTION_BOOT_COMPLETED广播后,启动你需要...

    后台保持运行,开机后自动启动设定好的APK的DEMO.zip

    这个"后台保持运行,开机后自动启动设定好的APK的DEMO.zip"是一个示例代码,用于教学如何在Android设备上实现在后台持续运行的应用以及设置应用在开机时自动启动。下面将详细讲解这两个知识点。 **1. 后台保持运行*...

    开机自启动并全屏浏览指定网址的安卓APP开发

    在这个项目中,开发者将创建一个Android应用,该应用在设备开机时自动启动,并且打开后会全屏显示特定的网页。 首先,我们需要了解**开机自启动**的概念。在Android系统中,可以使用BroadcastReceiver监听到设备...

    开机自动启动应用

    在Android系统中,实现“开机自动启动应用”的功能通常涉及到几个关键的概念和技术,包括广播接收器(BroadcastReceiver)、意图(Intent)以及应用权限管理。下面将详细解释这些知识点。 首先,**广播接收器...

    android Activity自启动

    "android Activity自启动"这个主题涉及到如何让一个Activity在设备开机时自动启动,这在一些需要后台服务或者持续监控的App中非常常见,比如天气应用、闹钟应用等。以下是关于这个主题的详细知识点: 1. **...

    Android代码-后台保持运行开机后自动启动设定好的APK的DEMO.zip

    在Android开发中,有时我们需要实现应用在用户开机后自动启动并保持后台运行的功能,这通常涉及到服务(Service)和BroadcastReceiver(广播接收器)的概念。本DEMO将演示如何实现这一功能,通过分析提供的文件我们...

    安卓实现开机自动进入应用程序

    在安卓系统中,让应用程序在开机后自动启动可以提高用户体验,特别是对于那些需要后台服务或者希望用户一打开手机就能看到最新信息的应用来说。本篇将详细介绍如何在安卓中实现开机自启动应用程序,并附带相关代码...

    Android应用源码之后台保持运行,开机后自动启动设定好的APK的DEMO.rar

    总之,这个DEMO项目涉及到了Android服务、开机启动、权限管理等多个关键知识点,对于希望学习如何使应用在后台持续运行并开机自动启动的开发者来说,是一个宝贵的教育资源。通过深入研究这个DEMO,可以更好地理解...

Global site tag (gtag.js) - Google Analytics