日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android开发日记】第一个任务Android Service!Service靴+重力感应器+弹出窗口+保持执行...

發(fā)布時間:2025/4/16 Android 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android开发日记】第一个任务Android Service!Service靴+重力感应器+弹出窗口+保持执行... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言:

????? 近期在寫一個小程序,需求是手機搖一搖就彈窗出來。第一次使用了Service,學(xué)習(xí)了兩天,實現(xiàn)了Service彈窗,開機啟動,Service啟動和銷毀,Service保持一直執(zhí)行。

滿足了自己的需求。現(xiàn)記錄學(xué)習(xí)心得。

希望能給你帶來一些幫助。


1.Service創(chuàng)建:重寫4個方法

  • onBind():返回一個IBinder對象,這個對象能夠使應(yīng)用程序與Service通信。假設(shè)用startService、stopService啟動和關(guān)閉Service的話。Service和訪問者是無法通信交換數(shù)據(jù)的。onBind()返回值設(shè)為null就可以。

    可是假設(shè)想要交換數(shù)據(jù),就要用bindService、unbindService來啟動和關(guān)閉Service。這時,onBind()要返回一個有效的IBinder對象。

  • onCreate():Service第一次被創(chuàng)建時調(diào)用此方法。
  • onStartCommand():理解為onStart()的新一代。每次通過startService(Intent)啟動Service時都會調(diào)用此方法。
  • onDestroy():Service被關(guān)閉之前調(diào)用此方法。

package com.service;import android.app.Service; import android.content.Context; import android.content.Intent; import android.util.Log;public class PopupService extends Service {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;} @Override public void onCreate() { super.onCreate(); System.out.println("Service is Created");} @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("Service is Started");return START_STICKY;}@Override public void onDestroy() { super.onDestroy(); System.out.println("Service is Destroyed");} }

2.Service配置:在AndroidManifest.xml中聲明

<service android:name="com.service.PopupService" android:priority = "1000" <!-- 提高優(yōu)先級-->android:persistent="true"> <!-- 免殺,不知道有沒有起作用--><intent-filter><action android:name="com.service.POPUP_SERVICE" /> </intent-filter> </service>
3.Service開機啟動:使用BroadcastReceiver

?? 文件創(chuàng)建:

package com.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class StartupReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 啟動一個Service Intent serviceIntent = new Intent(context, PopupService.class); context.startService(serviceIntent); } }
??? 文件配置:在AndroidManifest.xml中聲明

<receiver android:name="com.service.StartupReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver>
??? 注意權(quán)限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

4.Service從Activity中啟動:

?? 在主Activity中使用startService(Intent)

Intent popupintent=new Intent(); popupintent.setAction("com.service.POPUP_SERVICE"); startService(popupintent);
5.Service監(jiān)聽重力感應(yīng)而且彈窗:

  • ?? 這里使用Sensor.TYPE_ACCELEROMETER加速度感應(yīng)器。和其它監(jiān)聽器比方手勢等一樣,包含聲明、注冊、監(jiān)聽等
  • ?? 彈窗使用的是theme定義為dialog。notitle的activity。

?? 彈窗相關(guān)代碼:


Intent activityIntent = new Intent(this, SelectFriendsActivity.class); //要想在Service中啟動Activity,必須設(shè)置例如以下標(biāo)志 activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(activityIntent);

?? 完整代碼:

package com.service;import com.task.SelectFriendsActivity;import android.app.Service; import android.content.Context; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.IBinder; import android.os.Vibrator; import android.util.Log;public class PopupService extends Service implements SensorEventListener{//sensorManagerprivate SensorManager sensorManager; private Vibrator vibrator; Intent activityIntent;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;} @Override public void onCreate() { super.onCreate(); System.out.println("Service is Created");sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("Service is Started");//啟動service,將serviceon置為TRUE,可彈窗。SelectFriendsActivity.serviceon = true;if (sensorManager != null) {// 注冊監(jiān)聽器 sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); // 第一個參數(shù)是Listener。第二個參數(shù)是所得傳感器類型。第三個參數(shù)值獲取傳感器信息的頻率 } activityIntent = new Intent(this, SelectFriendsActivity.class); // 要想在Service中啟動Activity,必須設(shè)置例如以下標(biāo)志 activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); return START_STICKY;}@Override public void onDestroy() { super.onDestroy(); System.out.println("Service is Destroyed");} /** * 重力感應(yīng)監(jiān)聽 */ public void onSensorChanged(SensorEvent event) { // 傳感器信息改變時運行該方法 float[] values = event.values; float x = values[0]; // x軸方向的重力加速度,向右為正 float y = values[1]; // y軸方向的重力加速度。向前為正 float z = values[2]; // z軸方向的重力加速度,向上為正 Log.i("group", "x軸方向的重力加速度" + x + ";y軸方向的重力加速度" + y + "。z軸方向的重力加速度" + z); // 一般在這三個方向的重力加速度達到40就達到了搖晃手機的狀態(tài)。 int medumValue = 19;// 三星 i9250怎么晃都不會超過20,沒辦法。僅僅設(shè)置19了 if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) { if(SelectFriendsActivity.serviceon){vibrator.vibrate(200); System.out.println("Service:shaked and popup!!!!!!!");startActivity(activityIntent); }else{System.out.println("Service:shaked only!!!!!!!");}} } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }

彈出的Activity就是一般的Activity,僅僅只是要在其xml中設(shè)置其大小,在AndroidManifest.xml中將其theme設(shè)置為notitle。dialog類型

能夠使用這個style:

<style name="dialogTheme" parent="android:Theme.Dialog"> <item name="android:windowNoTitle">true</item></style>

彈窗要求加入權(quán)限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

6.Service 保持一直執(zhí)行,不被殺死 的方法:

?? 重寫onDestroy():

@Override public void onDestroy() { // super.onDestroy(); // System.out.println("Service is Destroyed");System.out.println("Service is Destroyed,and is Restarted");Intent localIntent = new Intent(); localIntent.setClass(this, PopupService.class); //銷毀時又一次啟動Service this.startService(localIntent); }

??? 這樣不管怎樣Service都一直在后臺執(zhí)行了。

7.圖文記錄:

7.1 啟動app,MainActivity中調(diào)用了startService(popupintent);

?? 結(jié)果:說明依次調(diào)用了Service中的onCreate()、onStartCommand()方法,Service開始執(zhí)行

?7.2 震動手機,Service響應(yīng)彈窗

? ? 結(jié)果:彈窗,Service正常執(zhí)行,重力感應(yīng)器正常執(zhí)行

7.3 殺死應(yīng)用進程,震動手機,Service仍然響應(yīng)彈窗

? ? 結(jié)果:彈窗,說明盡管應(yīng)用進程被殺死,可是Service仍保持正常執(zhí)行,重力感應(yīng)器正常執(zhí)行

7.4 點擊彈窗的Avtivity中的button,button監(jiān)聽代碼:

Intent popupintent=new Intent(); popupintent.setAction("com.service.POPUP_SERVICE"); stopService(popupintent);即調(diào)用了stopService(Intent) 方法

結(jié)果:打印出Service is Destroyed,說明調(diào)用了Service的onDestroy()方法

7.5 再次震動手機,發(fā)現(xiàn)還是有打印輸出:

?? 結(jié)果:說名盡管調(diào)用了onDestroy()方法。可是其Context未被清除,Service仍然存在

7.6 殺死全部有關(guān)此彈窗的進程。再次震動手機。發(fā)現(xiàn)沒有打印輸出了:

??? 結(jié)果:一旦其Context被全然清除。Service就真正停止了。



感謝newcj 的博客 文章 Android 中的 Service 全面總結(jié) 讓我受益匪淺。大家能夠去看看,評論區(qū)非常精彩:




第一次學(xué)著用Service,文中假設(shè)有錯誤請大家指正。



?




版權(quán)聲明:本文博客原創(chuàng)文章,博客,未經(jīng)同意,不得轉(zhuǎn)載。

總結(jié)

以上是生活随笔為你收集整理的【Android开发日记】第一个任务Android Service!Service靴+重力感应器+弹出窗口+保持执行...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。