android jni示例_Android服务示例

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)

Android Services Life Cycle


Image source: Google

图片来源:Google

服务中使用的方法 (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”活动创建原始文件夹。

Services example in Android

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

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

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();
}
}

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.

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

Services in Android (Example)

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

android jni示例

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377242.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《MySQL——选错索引,该如何做》

如果不断地删除历史数据和新增数据&#xff0c;MySQL有时会选错索引。 选择索引是优化器的工作&#xff0c;优化器优化时会考虑的因素&#xff1a;扫描行数、是否需要排序、是否使用临时表 MySQL通过统计索引上的基数&#xff0c;作为索引的区分度。 统计方法时采样统计&#x…

LPWSTR 类型的实参与const.char *类型形参不兼容

CString csPlus; CString csSummand; m_PlusNumber.GetWindowTextW(csPlus); m_Summand.GetWindowTextW(csSummand); int nPlus atoi(csPlus.GetBuffer(0)); //将编辑框文本转换成整数// int nPlus atoi(strcpy(csPlus.GetBuffer(10),"aa")); csPlus.ReleaseBu…

空间换时间,把递归的时间复杂度降低到O(2n)

递归算法的时间复杂度除非只有前两项&#xff0c;否则都不是线性的&#xff0c;并且相当耗费内存。我们用最常见的的fibonacci数列来说明&#xff1a; function fibonacci(n){if( n 0 || n 1){return n;} else {return fibonacci(n - 1) fibonacci(n - 2);} } 这是一种最常见…

scala char_Scala中的Char数据类型

scala charScala Char数据类型 (Scala Char Data Type) Character (char) in Scala is a data type that is equivalent to 16-bit unsigned integer. The character data type stores a single character. It can be an alphabet, numbers, symbols, etc. The character takes…

《MySQL——给长字符串加索引》

对于长字符串&#xff0c;可用如下方式建立索引&#xff1a; &#xff08;1&#xff09;前缀索引 &#xff08;2&#xff09;字符串倒叙前缀索引 &#xff08;3&#xff09;添加hash字段并在hash字段上加索引 &#xff08;4&#xff09;字段拆分&#xff08;一个字段可拆分为两…

[蓝桥杯历届试题] 欧拉与鸡蛋

大数学家欧拉在集市上遇到了本村的两个农妇&#xff0c;每人跨着个空篮子。她们和欧拉打招呼说两人刚刚卖完了所有的鸡蛋。 欧拉随便问&#xff1a;“卖了多少鸡蛋呢&#xff1f;” 不料一个说&#xff1a;“我们两人自己卖自己的&#xff0c;一共卖了150个鸡蛋&#xff0c;虽然…

Python元组练习

Here, we are covering following Python tuple exercises, 在这里&#xff0c;我们将介绍以下Python元组练习 &#xff0c; Creating & printing a tuple 创建和打印元组 Unpacking the tuple into strings 将元组解包成字符串 Create a tuple containing the letters of…

傻瓜教你看清MVC内部执行流程之ViewData数据传输,轻松学MVC--①目了然篇(待续)

1.首先在执行到Controller里面的action(方法)时,执行到最后会调用一个View()-->此方法是Controller的一个方法 源代码: View Code protected internal ViewResult View(){return View(null /* viewName */, null /* masterName */, null /* model */);} 2.然后继续调用自己…

《MySQL——count()逻辑》

count()用法 count()语义&#xff1a;该函数为一个聚合函数&#xff0c;对于返回的结果集一行行地判断&#xff0c;如果count函数地参数不是NULL&#xff0c;累计值就加1&#xff0c;否则不加。最后返回累计值。 所以count(*),count(主键id)和count(1)都表示返回满足条件地结果…

phpmailer 发送邮件

<?php /* 可用新浪和网易邮箱测试成功&#xff0c;但QQ不成功&#xff01; 下载 phpmailer 解压 http://phpmailer.worxware.com/要注意邮件服务器的端口号&#xff0c;默认是 25 不用修改&#xff0c;如果不是则要修改如下&#xff0c;在$mail->IsSMTP() ;下一行加上 $…

静态负载均衡和动态负载均衡_动态负载平衡

静态负载均衡和动态负载均衡动态负载平衡 (Dynamic Load Balancing) The algorithm monitors changes on the system workload and redistributes the work accordingly. 该算法监视系统工作负载的变化并相应地重新分配工作。 This algorithm works on three strategies: 该算…

poj 1088

题目&#xff1a;http://poj.org/problem?id1088 记忆化搜索&#xff0c;dp[r][c] max(dp[r - 1][c] , dp[r 1][c] , dp[r][c - 1] , dp[r][c 1]) 1 ( if (题目给的条件满足&#xff09;&#xff09; View Code 1 using namespace std;2 typedef long long ll;3 const in…

《MySQL——order by逻辑(全字段排序与rowid排序)》

创建一个表&#xff0c;然后使用查询语句&#xff1a; 查询城市是“杭州”的所有人名字&#xff0c;并且按照姓名排序返回前 1000 个人的姓名、年龄 create table t (id int(11) not null,city vachar(16) not null,name vachar(16) not null,age vachar(16) not null,addr va…

ruby 生成哈希值_哈希== Ruby中的运算符

ruby 生成哈希值In the last article, we have seen how we can compare two hash objects with the help of < operator? "<" method is a public instance method defined in Rubys library. 在上一篇文章中&#xff0c;我们看到了如何在<运算符的帮助下…

HTML5 video

摘要&#xff1a;本文主要介绍HTML5 video在android2.2中实现的主要架构和程序流程。 一、实现HTML5 video主要的类 1&#xff0e; 主要类结构及介绍 图1中绿色类为java类&#xff0c;其余为c类&#xff0c;下面是各个类的具体介绍: (1) HTMLElement类不是最上层类&#xff0c…

《MySQL——使用联合索引、覆盖索引,避免临时表的排序操作》

联合索引避免临时表排序 在上一篇笔记(MySQL——order by逻辑&#xff08;全字段排序与rowid排序&#xff09;)中&#xff0c;讲到查询语句查询多个字段的时候使用order by语句实现返回值是有序的&#xff0c;而order by是使用到了临时表的&#xff0c;会带来时间和空间损失。…

明源面试

明源面试&#xff0c;笔试题目如下 一、SQL测试题 1 有两张表 根据给出的SQL语句&#xff0c;写出返回的行数分别是多少&#xff1f;为了形象直观的显示&#xff0c;我给出了sql语句执行结果。 A 学生表 B分数表 新题目 select a.* from a inner join b on a.idb.id; …

AEAP的完整形式是什么?

AEAP&#xff1a;尽早 (AEAP: As Early As Possible) AEAP is an abbreviation of "As Early As Possible". AEAP是“ April越早”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Faceboo…

jquery 视觉特效(鼠标悬停时依次显示图片)

效果描述&#xff1a; 有几副图片&#xff0c;让他们依次叠加重合。首先显示第一张图片。然后鼠标悬停在上面&#xff0c;边框变化。然后离开&#xff0c;然后第一张淡出&#xff0c;第二张淡入。接着悬停在第二张图片&#xff0c;边框变化&#xff0c;然后离开&#xff0c;第二…

《MySQL tips:查询时,尽量不要对字段进行操作》

维护一个交易系统&#xff0c;交易记录表tradelog包含交易流水号(tradeid)、交易员id(operator)、交易时间(t_modified)等字段。 建表语句如下&#xff1a; create table tradelog (id int(11) not null,tradeid varchar(32) default null,operator int(11) default null,t_mo…