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.net.inetaddress