欢迎访问 生活随笔!

生活随笔

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

编程问答

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

发布时间:2025/4/16 编程问答 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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安全用法及代码示例...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。