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:
这有两种类型:
Started
已开始
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
服务中使用的方法 (Methods used in Services)
onStartCommand()
onStartCommand()
This method is called, when an activity wish to start a service by calling
当活动希望通过调用来启动服务时,调用此方法
startService().
startService() 。
onBind()
onBind()
This method is called when another component such as an activity wish to bind with the service by calling
当其他组件(例如活动)希望通过调用与服务绑定时,将调用此方法
bindService().
bindService() 。
onUnbind()
onUnbind()
This method is called, when all components such as clients got disconnected from an interface.
当所有组件(例如客户端)与接口断开连接时,将调用此方法。
onRebind()
onRebind()
This method is called, when new clients connect to service.
新客户端连接到服务时,将调用此方法。
OnCreate()
OnCreate()
This method is called when the service is first created using
首次使用创建服务时调用此方法
onStartCommand() or onBind().
onStartCommand()或onBind() 。
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相关的方法的实现。
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.
在虚拟设备上执行代码后,将获得以下输出。
翻译自: https://www.includehelp.com/android/services-example.aspx
android jni示例