android 49 广播接收者中启动其他组件
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btnStartActivity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动 activity" /> <Button android:id="@+id/btnStartService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动service" /> <Button android:id="@+id/btnStopService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止 service" /> </LinearLayout>
mainActivity.java
package com.sxt.day07_05; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setListener(); } private void setListener() { findViewById(R.id.btnStartActivity).setOnClickListener(this); findViewById(R.id.btnStartService).setOnClickListener(this); findViewById(R.id.btnStopService).setOnClickListener(this); } @Override public void onClick(View v) { Intent intent=new Intent(); switch (v.getId()) { case R.id.btnStartActivity: intent.setAction("com.sxt.day07_05.start_activity");//发送一条广播,被MyReceiver接收到, break; case R.id.btnStartService: intent.setAction("com.sxt.day07_05.start_service"); break; case R.id.btnStopService: intent.setAction("com.sxt.day07_05.stop_service"); break; } sendBroadcast(intent);//发送广播 } }
广播接收者:
package com.sxt.day07_05; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {//收到广播 String action=intent.getAction(); Intent intent2=null; if(action.equals("com.sxt.day07_05.start_activity")){ //启动SecondActivity intent2=new Intent(context, SecondActivity.class);//context是上下文,就是发送广播的Activity intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//标志,一个Activity启动另一个Activity不用这个,因为2个Activity处于同一个任务栈,广播接收者没有任务栈,所以必出把SecondActivity启动在一个新的任务栈。因为每个Activity都在任务栈里面。 context.startActivity(intent2); }else if(action.equals("com.sxt.day07_05.start_service")){ intent2=new Intent(context, MyService.class); context.startService(intent2);//启动service }else if(action.equals("com.sxt.day07_05.stop_service")){ intent2=new Intent(context, MyService.class); context.stopService(intent2);//停止service } } }
广播接收者启动的service:
package com.sxt.day07_05; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.SystemClock; import android.util.Log; public class MyService extends Service { boolean mLooper=true;//true:循环继续 @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override //service是一个后台进程,onBind,onStartCommand,onDestroy以on开头的方法都是在主线程执行的,所以为了不占用主线程的时间,这些方法里面的代码都要开辟工作线程执行。 public int onStartCommand(Intent intent, int flags, int startId) { //耗时操作都要在工作线程里面写 //即使这个service停止了,但是这个工作线程仍然不会停, //在Activity和service写的工作线程,即使Activity和service销毁了,这个工作线程还在执行。 new Thread(){ public void run() { while(mLooper){ SystemClock.sleep(1000); Log.i("main","service is loop..."); } }; }.start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); mLooper=false;//service销毁工作线程不会停止,因此要mLooper=false } }
广播接收者启动的Activity:
package com.sxt.day07_05; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.second, menu); return true; } } <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SecondActivity" /> </RelativeLayout>
系统描述文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sxt.day07_05" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.sxt.day07_05.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.sxt.day07_05.MyReceiver" > 一个广播接收者接收3个action,3个字符串,3个频道的广播, <intent-filter> <action android:name="com.sxt.day07_05.start_activity" /> <action android:name="com.sxt.day07_05.start_service" /> <action android:name="com.sxt.day07_05.stop_service" /> </intent-filter> </receiver> <service android:name="com.sxt.day07_05.MyService" /> 声明service <activity 声明activity android:name="com.sxt.day07_05.SecondActivity" android:label="@string/title_activity_second" > </activity> </application> </manifest>