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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

private用法 java_关于android开发中如何正确使用Private Services安全用法及代码示例...

發布時間:2025/4/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 private用法 java_关于android开发中如何正确使用Private Services安全用法及代码示例... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、注意事項1、顯式設置exported屬性為false。@b@2、安全處理收到的intent,確認其真實性。@b@3、敏感數據可以在同一個應用中發送和請求。

二、原代碼示例

1.AndroidManifest.xml<?xml ?version="1.0"?encoding="utf-8"?>@b@?@b@@b@?@b@????@b@????????@b@????????????@b@????????????????@b@????????????????@b@????????????@b@????????@b@?@b@????????@b@????????@b@????????@b@?@b@????????@b@????????@b@????????@b@?@b@????????@b@

2.PrivateStartService.javapackage?org.jssec.android.service.privateservice;@b@?@b@import?android.app.Service;@b@import?android.content.Intent;@b@import?android.os.IBinder;@b@import?android.widget.Toast;@b@?@b@public?class?PrivateStartService?extends?Service?{@b@?@b@????//?The?onCreate?gets?called?only?one?time?when?the?service?starts.@b@????@Override@b@????public?void?onCreate()?{@b@????????Toast.makeText(this,?"PrivateStartService?-?onCreate()",?Toast.LENGTH_SHORT).show();@b@????}@b@?@b@????//?The?onStartCommand?gets?called?each?time?after?the?startService?gets?called.@b@????@Override@b@????public?int?onStartCommand(Intent?intent,?int?flags,?int?startId)?{@b@????????//?***?POINT?2?***?Handle?the?received?intent?carefully?and?securely,@b@????????//?even?though?the?intent?was?sent?from?the?same?application.@b@????????//?Omitted,?since?this?is?a?sample.?Please?refer?to?"3.2?Handling?Input?Data?Carefully?and?Securely."@b@????????String?param?=?intent.getStringExtra("PARAM");@b@????????Toast.makeText(this,@b@????????????String.format("PrivateStartService¥nReceived?param:?¥"%s¥"",?param),?Toast.LENGTH_LONG).show();@b@????????return?Service.START_NOT_STICKY;@b@????}@b@?@b@????//?The?onDestroy?gets?called?only?one?time?when?the?service?stops.?@Override@b@????public?void?onDestroy()?{@b@????????Toast.makeText(this,?"PrivateStartService?-?onDestroy()",?Toast.LENGTH_SHORT).show();@b@????}@b@?@b@????@Override@b@????public?IBinder?onBind(Intent?intent)?{?@b@????????//?This?service?does?not?provide?binding,?so?return?null@b@????????return?null;@b@????}@b@}

3.安全使用PrivateUserActivity.java - (1、在同一個程序中,使用顯式intent調用service、2、第三信息可以發送給同一個應用中的目標service、3、處理收到的結果數據,確認真實性和可用性)package?org.jssec.android.service.privateservice;@b@?@b@import?android.app.Activity;@b@import?android.content.Intent;@b@import?android.os.Bundle;@b@import?android.view.View;@b@?@b@public?class?PrivateUserActivity?extends?Activity?{@b@?@b@????@Override@b@????public?void?onCreate(Bundle?savedInstanceState)?{@b@????????super.onCreate(savedInstanceState);@b@????????setContentView(R.layout.privateservice_activity);@b@????}@b@?@b@????//?---?StartService?control?---@b@?@b@????public?void?onStartServiceClick(View?v)?{@b@????????//?***?POINT?4?***?Use?the?explicit?intent?with?class?specified?to?call?a?service?in?the?same?application.@b@????????Intent?intent?=?new?Intent(this,?PrivateStartService.class);@b@?@b@????????//?***?POINT?5?***?Sensitive?information?can?be?sent?since?the?destination?service?is?in?the?same?application.@b@????????intent.putExtra("PARAM",?"Sensitive?information");@b@?@b@????????startService(intent);@b@????}@b@?@b@????public?void?onStopServiceClick(View?v)?{@b@????????doStopService();@b@????}@b@?@b@????@Override@b@????public?void?onStop()?{@b@????????super.onStop();@b@????????//?Stop?service?if?the?service?is?running.@b@????????doStopService();@b@????}@b@?@b@????private?void?doStopService()?{@b@????????//?***?POINT?4?***?Use?the?explicit?intent?with?class?specified?to?call?a?service?in?the?same?application.@b@????????Intent?intent?=?new?Intent(this,?PrivateStartService.class);@b@????????stopService(intent);@b@????}@b@?@b@????//?---?IntentService?control?---@b@?@b@????public?void?onIntentServiceClick(View?v)?{@b@????????//?***?POINT?4?***?Use?the?explicit?intent?with?class?specified?to?call?a?service?in?the?same?application.@b@????????Intent?intent?=?new?Intent(this,?PrivateIntentService.class);@b@?@b@????????//?***?POINT?5?***?Sensitive?information?can?be?sent?since?the?destination?service?is?in?the?same?application.@b@????????intent.putExtra("PARAM",?"Sensitive?information");@b@?@b@????????startService(intent);@b@????}@b@}

三、安全代碼示例1、在同一個程序中,使用顯式intent調用service。@b@2、第三信息可以發送給同一個應用中的目標service。@b@3、處理收到的結果數據,確認真實性和可用性。PrivateUserActivity.java@b@?@b@package?org.jssec.android.service.privateservice;@b@?@b@import?android.app.Activity;@b@import?android.content.Intent;@b@import?android.os.Bundle;@b@import?android.view.View;@b@?@b@public?class?PrivateUserActivity?extends?Activity?{@b@?@b@????@Override@b@????public?void?onCreate(Bundle?savedInstanceState)?{@b@????????super.onCreate(savedInstanceState);@b@????????setContentView(R.layout.privateservice_activity);@b@????}@b@?@b@????//?---?StartService?control?---@b@?@b@????public?void?onStartServiceClick(View?v)?{@b@????????//?***?POINT?4?***?Use?the?explicit?intent?with?class?specified?to?call?a?service?in?the?same?application.@b@????????Intent?intent?=?new?Intent(this,?PrivateStartService.class);@b@?@b@????????//?***?POINT?5?***?Sensitive?information?can?be?sent?since?the?destination?service?is?in?the?same?application.@b@????????intent.putExtra("PARAM",?"Sensitive?information");@b@?@b@????????startService(intent);@b@????}@b@?@b@????public?void?onStopServiceClick(View?v)?{@b@????????doStopService();@b@????}@b@?@b@????@Override@b@????public?void?onStop()?{@b@????????super.onStop();@b@????????//?Stop?service?if?the?service?is?running.@b@????????doStopService();@b@????}@b@?@b@????private?void?doStopService()?{@b@????????//?***?POINT?4?***?Use?the?explicit?intent?with?class?specified?to?call?a?service?in?the?same?application.@b@????????Intent?intent?=?new?Intent(this,?PrivateStartService.class);@b@????????stopService(intent);@b@????}@b@?@b@????//?---?IntentService?control?---@b@?@b@????public?void?onIntentServiceClick(View?v)?{@b@????????//?***?POINT?4?***?Use?the?explicit?intent?with?class?specified?to?call?a?service?in?the?same?application.@b@????????Intent?intent?=?new?Intent(this,?PrivateIntentService.class);@b@?@b@????????//?***?POINT?5?***?Sensitive?information?can?be?sent?since?the?destination?service?is?in?the?same?application.@b@????????intent.putExtra("PARAM",?"Sensitive?information");@b@?@b@????????startService(intent);@b@????}@b@}

總結

以上是生活随笔為你收集整理的private用法 java_关于android开发中如何正确使用Private Services安全用法及代码示例...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。