Android 核心技术

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中注册

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.byread" android:versionCode="1" android:versionName="1.0">  
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  5.         <activity android:name=".MainActivity" android:label="@string/app_name">  
  6.             <intent-filter>  
  7.                 <action android:name="android.intent.action.MAIN" />  
  8.                 <category android:name="android.intent.category.LAUNCHER" />  
  9.             </intent-filter>  
  10.         </activity>  
  11.         <!-- 注册自定义静态广播接收器 -->  
  12.         <receiver android:name=".StaticReceiver">  
  13.             <intent-filter>  
  14.                 <action android:name="包名" />  
  15.             </intent-filter>  
  16.         </receiver>  
  17.         <!-- 注册系统静态广播接收器 -->  
  18.         <receiver android:name=".SystemReceiver">  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.BATTERY_LOW" />  
  21.             </intent-filter>  
  22.         </receiver>  
  23.     </application>  
  24. </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>
############################################################################

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值