android jni示例

A service is a component that runs in the background for supporting different types of operations that are long running. The user is not interacted with these. These perform task even if application is destroyed. Examples include handling of network transactions, interaction with content providers, playing music.

服务是在后台运行的组件,用于支持长时间运行的不同类型的操作。 用户未与这些人互动。 即使应用程序被破坏,它们也会执行任务。 示例包括网络交易的处理,与内容提供商的交互,播放音乐。

This is of two types:

这有两种类型:

  1. Started

    已开始

  2. Bound

1)开始服务 (1) Started Service)

A service is started when startService() method is called by an activity. It continues to run in the background. It is stopped when stopService() method is called.

当活动调用startService()方法时,将启动服务。 它继续在后台运行。 当调用stopService()方法时,它将停止。

2)绑定服务 (2) Bound Service)

A service is bound when bindService() method is called by an activity. When unbindService() method is called the component is unbind.

当活动调用bindService()方法时,将绑定服务。 调用unbindService()方法时,组件将解除绑定。

启动和绑定服务示例 (Example of Started and Bound services)

For instance I play audio in background, startService() method is called. When I try to get information of current audio file, I shall bind that service that provides information regarding current audio.

例如,我在后台播放音频,则调用startService()方法。 当我尝试获取当前音频文件的信息时,我将绑定提供有关当前音频信息的服务。

Android服务生命周期 (Android Services Life Cycle)

Image source: Google

图片来源:Google

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

服务中使用的方法 (Methods used in Services)

  1. onStartCommand()

    onStartCommand()

    This method is called, when an activity wish to start a service by calling

    当活动希望通过调用来启动服务时,调用此方法

    startService().

    startService() 。

  2. onBind()

    onBind()

    This method is called when another component such as an activity wish to bind with the service by calling

    当其他组件(例如活动)希望通过调用与服务绑定时,将调用此方法

    bindService().

    bindService() 。

  3. onUnbind()

    onUnbind()

    This method is called, when all components such as clients got disconnected from an interface.

    当所有组件(例如客户端)与接口断开连接时,将调用此方法。

  4. onRebind()

    onRebind()

    This method is called, when new clients connect to service.

    新客户端连接到服务时,将调用此方法。

  5. OnCreate()

    OnCreate()

    This method is called when the service is first created using

    首次使用创建服务时调用此方法

    onStartCommand() or onBind().

    onStartCommand()或onBind() 。

  6. onDestroy()

    onDestroy()

    This method is called when the service is being destroyed.

    销毁服务时将调用此方法。

Example - Draw three buttons to start, stop and move to next page.

示例-绘制三个按钮以开始,停止并移至下一页。

1) XML File: activity_main

1)XML文件:activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity" >
<Button
android:id="@+id/buttonStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Start Your Service" />
<Button
android:id="@+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonNext"
android:layout_alignRight="@+id/buttonStart"
android:layout_marginBottom="35dp"
android:text="Stop Your Service" />
<Button
android:id="@+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonStop"
android:layout_centerVertical="true"
android:text="Next Activity" />
</android.support.constraint.ConstraintLayout>

Main Activity includes startService() and stopService() methods to start and stop the service.

Main Activity包含用于启动和停止服务的startService()和stopService()方法。

2) Java File: MainActivity.java

2)Java文件:MainActivity.java

package com.example.faraz.testing;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {Button buttonStart, buttonStop,buttonNext;
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonNext = (Button) findViewById(R.id.buttonNext);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonNext.setOnClickListener(this);
}
public void onClick(View src) {switch (src.getId()) {case R.id.buttonStart:
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
stopService(new Intent(this, MyService.class));
break;
case R.id.buttonNext:
Intent intent=new Intent(this,NextPage.class);
startActivity(intent);
break;
}
}
}

You have to create raw folder in your res directory for "Myservice.java" activity.

您必须在res目录中为“ Myservice.java”活动创建原始文件夹。

MyService.java includes implementation of methods associated with Service based on requirements.

MyService.java包括基于需求的与Service相关的方法的实现。

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

3) MyService.java

3)MyService.java

package com.example.faraz.testing;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {MediaPlayer myPlayer;
@Override
public IBinder onBind(Intent intent) {return null;
}
@Override
public void onCreate() {Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
myPlayer = MediaPlayer.create(this, R.raw.sun);// paste your audio in place of sun file
myPlayer.setLooping(false); // Set looping
}
@Override
public void onStart(Intent intent, int startid) {Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
myPlayer.start();
}
@Override
public void onDestroy() {Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
myPlayer.stop();
}
}
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

4) XML File: activity_nextpage.xml

4)XML文件:activity_nextpage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="96dp"
android:layout_marginTop="112dp"
android:text="Next Page" />
</LinearLayout>

5) NextPage.java

5)NextPage.java

package com.example.faraz.testing;
import android.app.Activity;
import android.os.Bundle;
public class NextPage extends Activity {@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
}
}

Output

输出量

After executing your code on your virtual device, you get following output.

在虚拟设备上执行代码后,将获得以下输出。

翻译自: https://www.includehelp.com/android/services-example.aspx

android jni示例

android jni示例_Android服务示例相关推荐

  1. android jni示例_Android动画示例

    android jni示例 Android Animation is used to give the UI a rich look and feel. Animations in android a ...

  2. android jni示例_Android GridLayoutManager示例

    android jni示例 Android GridLayoutManager is the RecyclerView.LayoutManager implementation to lay out ...

  3. android jni示例_Android TextInputLayout示例

    android jni示例 In this tutorial, we'll be looking in depth at the features that Android TextInputLayo ...

  4. android jni示例_Android CollapsingToolbarLayout示例

    android jni示例 Welcome to Android CollapsingToolbarLayout Example. In this tutorial, we'll discuss an ...

  5. android实例教程_Android ConstraintLayout示例教程

    android实例教程 In this tutorial, we'll discuss the intricacies of android ConstraintLayout. Google had ...

  6. android jni示例_Android切换按钮,开关示例

    android jni示例 Today we will learn about Android Toggle Button and Switch in android app. We'll discu ...

  7. HIDL示例-JAVA服务创建-Client验证-Android10.0 HwBinder通信原理(四)

    摘要:本节主要来讲解Android10.0 JAVA层的HIDL服务创建和JAVA层的Client验证 阅读本文大约需要花费15分钟. 文章首发微信公众号:IngresGe 专注于Android系统级 ...

  8. HIDL示例-C++服务创建Client验证-Android10.0 HwBinder通信原理(三)

    摘要:本节主要来讲解Android10.0 Native层的HIDL服务创建和Native层的Client验证 阅读本文大约需要花费18分钟. 文章首发微信公众号:IngresGe 专注于Androi ...

  9. 安卓应用安全指南 4.4.1 创建/使用服务 示例代码

    4.4.1 创建/使用服务 示例代码 原书:Android Application Secure Design/Secure Coding Guidebook 译者:飞龙 协议:CC BY-NC-SA ...

最新文章

  1. 从零到有的突破:BCH爱好者聚集地BCH.Club公测上线
  2. javaweb项目中的过滤器的使用
  3. 邮件附件在线预览——HTML Filter
  4. 各数据结构算法时间复杂度图【笔记自用】
  5. 【学生信息管理系统】——优化篇(二)
  6. Website for the introduction to Matlab and Java
  7. 算法只能应用于计算机吗,把你的原则转换成算法,让计算机和你一起决策
  8. cad一键标注闭合区域lisp_CAD快捷键大全,你值得学会!
  9. 修改拦截器里的请求头_OkHttp4 源码分析(1) 请求流程分析
  10. discuz mysql语句_discuz 数据库插入
  11. 【快递100】 物流公司对应编码分享(截止到2021-09-19 最新数据)
  12. 阿里巴巴达摩院发布2019十大科技趋势:语音AI在特定领域通过图灵测试
  13. Mp3帧分析(数据帧)
  14. 多元微积分_向量函数偏导
  15. 用微分和差分方程描述的因果LIT系统
  16. 原始套接(ARP协议的使用)
  17. EasyExcel读写Excel
  18. 【持续更新-34家】百亿量化私募名单-2022年7月27日
  19. 计算机网络基础教程实训总结,实训总结
  20. 【JSP/SERVLET】Tomcat内置表单身份验证

热门文章

  1. 常见的清除浮动的五种解决办法
  2. 输出空格隔开换行_【前端干货】CSS 的空格处理
  3. BOM之navigator对象和用户代理检测
  4. Two.js – 为现代浏览器而生的 2D 绘图 API
  5. Android Studio 管理所有程序退出
  6. Unity3D_(游戏)2D坦克大战 像素版
  7. Deepin安装Curl的方法
  8. px ,em ,rem
  9. 大话设计模式读书笔记--6.原型模式
  10. 【转】phpize学习