1.service组件后台服务:service一般有activity启动,service具有自己较长的生命周期,当activity结束后,service不会结束,当service自己的生命周期结束时才会结束。
service的两种那个启动方式:startservice,bindservice
service后台服务的用法:
1.都需要在Androidmanifest.xml中声明,格式如下
<service android:enabled=["true" | "false"]
android:exported=["true" | "false"]
android:icon="drawable resource"
android:isolatedProcess=["true" | "false"]
android:label="string resource"
android:name="string"
android:permission="string"
android:process="string" >
. . .
</service>
2.需要子类继承service,并需要重写onbind(),oncreate(),onstratcommand(),ondestory()四个方法。
3.在其他activity中启动service后台服务:直接调用startservice(Intent)即可,
停止service服务调用stoopservice(Intent)
如下:
Intent serviceIntent = new Intent(MainActivity.this, MyService.class);
24 startService(serviceIntent);
其中MyService就是继承了service的子类。
######################################################################################################
自定义service服务调用代码:
activity调用:
button0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this, MyService.class); intent.putExtra("msg","启动service成功"); intent.setAction("MyService"); startService(intent); } });
androidifest.xml注册:
<service android:name="com.android.zhujian.MyService"> <intent-filter> <action android:name="com.android.zhujian"></action> </intent-filter> </service>
布局:
<Button android:layout_width="match_parent" android:id="@+id/btn0" android:text="自定义启动service" android:layout_height="50dp" tools:ignore="HardcodedText"/>
继承service:
package com.android.zhujian; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; /** * Created by Administrator on 2017/7/26. */ public class MyService extends Service { @Override public IBinder onBind(Intent intent) { Toast.makeText(getBaseContext(),"onBind",Toast.LENGTH_LONG).show(); return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(getBaseContext(),intent.getStringExtra("msg"),Toast.LENGTH_LONG).show(); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); Toast.makeText(getBaseContext(),"oncreate",Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(getBaseContext(),"ondestory",Toast.LENGTH_LONG).show(); } }##############################################################################
2.BroadcastReceiver组件广播发送与接收实现:
第一步:需要new一个BroadcastReceiver并重写onreceive方法
第二步:需要在其他activity中注册广播:直接调用registerReceiver(BroadcastReceiver,IntentFilter)
其中的braodcasrreceiver中的onreceive()就是接收广播
第三步:发送广播:直接调用sendBroadCast(Intent)
第四步:在androidifest.xml中注册
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.byread" android:versionCode="1" android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".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=".StaticReceiver">
- <intent-filter>
- <action android:name="包名" />
- </intent-filter>
- </receiver>
- <!-- 注册系统静态广播接收器 -->
- <receiver android:name=".SystemReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BATTERY_LOW" />
- </intent-filter>
- </receiver>
- </application>
- </manifest>
###########################################################################
代码注册:
package com.example.administrator.andriod_four_zujian; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import com.android.zhujian.StaticBroadCast; public class MainActivity extends AppCompatActivity { private Button button; private BroadcastReceiver broadcastReceiver=new StaticBroadCast(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.btn1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("info", "onClick: "+13); Intent intent=new Intent(); intent.setAction("staticBroadCast"); intent.putExtra("msg","静态广播接收成功"); sendBroadcast(intent); } }); } @Override protected void onStart() { super.onStart(); IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction("staticBroadCast"); registerReceiver(broadcastReceiver,intentFilter); } }
package com.android.zhujian; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * Created by Administrator on 2017/7/26. */ public class StaticBroadCast extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("staticBroadCast")){ String msg= intent.getStringExtra("msg"); Toast.makeText(context,msg,Toast.LENGTH_LONG).show(); } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.andriod_four_zujian.MainActivity"> <Button android:layout_width="match_parent" android:id="@+id/btn1" android:text="自定义静态注册发送广播" android:layout_height="wrap_content" tools:ignore="HardcodedText"/> </RelativeLayout>
############################################################################