配置文件如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="net.blogjava.mobile.startupservice" android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name">    <activity android:name=".MessageActivity"      android:theme="@android:style/Theme.Dialog"><intent-filter><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><receiver android:name="StartupReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></receiver><service android:enabled="true" android:name=".MyService" /></application><uses-sdk android:minSdkVersion="3" /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /></manifest> 


定义广播接收器:
public class StartupReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent){Intent serviceIntent = new Intent(context, MyService.class);context.startService(serviceIntent);Intent activityIntent = new Intent(context, MessageActivity.class);//要想在Service中启动Activity ,必须设置如下标志activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(activityIntent);}}


定义后台服务类:
public class MyService extends Service{@Overridepublic IBinder onBind(Intent intent){return null;}@Overridepublic void onCreate(){Log.d("StartUpMyService", "onCreate");super.onCreate();}@Overridepublic void onDestroy(){Log.d("StartUpMyService", "onDestroy");super.onDestroy();}@Overridepublic void onStart(Intent intent, int startId){Log.d("StartUpMyService", "onStart");super.onStart(intent, startId);}}


定义绑定的Activity
public class MessageActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.message);}}

更多相关文章

  1. Android 中自定义 Adapter
  2. Android 系统菜单与自定义菜单
  3. Android 基础知识、Android 进阶知识、Android 自定义 View 相关
  4. android自定义属性的使用
  5. Android 中自定义属性(attr.xml,TypedArray)的使用
  6. ANDROID自定义SCROLLVIEW
  7. 《Gradle 权威指南》读书笔记——第八章 自定义Android Gradle
  8. Android ndk开发时查看编译器预定义宏

随机推荐

  1. [007] 如何在Android应用中使用百度地图a
  2. NDK下 将Platinum SDK 编译成so库 (androi
  3. android 世界各国英文简写代码 资源文件
  4. [SL4A]获取IMEI信息
  5. Android消息队列原理
  6. 使用jsoup爬取数据实现android课程表
  7. Android使用Canvas绘制圆形进度条效果
  8. Android(安卓)Studio 开启支持 VectorDra
  9. AndroidStudio 2.0 导入(.so)文件方法,以
  10. Android(安卓)NDK JNI 入门笔记-day02-基