Android 核心技术
来源:互联网 发布:sniffer linux 编辑:程序博客网 时间:2024/06/04 19:00
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>
############################################################################
- Android 核心技术
- android 核心技术分析-全篇
- Android 插件化 核心技术
- Android核心技术之(1)---android快速入
- Android核心技术:Dalvik 虚拟机移植指导
- android核心技术之性能分析工具TraceView
- android核心技术之性能分析工具Systrace
- android核心技术与最佳实践笔记(一)
- android核心技术与最佳实践笔记(二)
- android核心技术与最佳实践笔记(三)
- Android核心技术之(4)---数据存储
- Android核心技术之(6)---事件机制
- Android核心技术之(10)---动画(Aimation)
- android核心技术之ANR分析(MTK)
- Android线程—多线程编程核心技术
- 《Android核心技术与实例详解》笔记第一篇
- 《Android核心技术与实例详解》笔记第二篇
- Android核心技术与实例详解(第2版)
- 第八届ACM校赛->魔戒
- 搜索关键字拼音智能提示实现
- 1959简单枚举类型——植物与颜色
- js实现首尾相连的文字滚动效果
- 淘淘商城(第一天)
- Android 核心技术
- C# 面向对象(Mp3实例完善 无图)
- HDU-1272-小希的迷宫 [并查集]
- 敏捷定义
- POJ 2456 Aggressive cows<二分贪心>
- 7.26
- 关于HttpSession前台无值后台有值的解决方法
- config.properties的配置
- Session和Cookie