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安全用法及代码示例...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python替换文本文件单词_在大型文本
- 下一篇: java gc会回收类么_Java GC