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:
這有兩種類型:
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
.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)
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" ><Buttonandroid: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" /><Buttonandroid: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" /><Buttonandroid: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;@Overridepublic 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;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic 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 filemyPlayer.setLooping(false); // Set looping}@Overridepublic void onStart(Intent intent, int startid) {Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();myPlayer.start();}@Overridepublic 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" ><TextViewandroid: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 {@Overridepublic 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服务示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中药柜多少钱啊?
- 下一篇: 请问现在淘宝上的二手笔记本联想T510加