安卓笔记—调用栈之从sendBroadcast到onReceive

以U之前版本的parallel广播为例

// 应用进程
android.content.ContextWrapper.sendBroadcast::Intent:
android.content.Context.sendBroadcast::Intent:
android.app.ContextImpl.sendBroadcast::Intent:
// binder 到system server进程
com.android.server.am.ActivityManagerService.broadcastIntentWithFeature::IApplicationThread,String,Intent,String,IIntentReceiver,int,String,Bundle,String[],String[],String[],int,Bundle,boolean,boolean,int:
com.android.server.am.ActivityManagerService.broadcastIntentLocked::ProcessRecord,String,String,Intent,String,ProcessRecord,IIntentReceiver,int,String,Bundle,String[],String[],String[],int,Bundle,boolean,boolean,int,int,int,int,int,BackgroundStartPrivileges,int[],BiFunction:
com.android.server.am.ActivityManagerService.broadcastIntentLockedTraced::ProcessRecord,String,String,Intent,String,ProcessRecord,IIntentReceiver,int,String,Bundle,String[],String[],String[],int,BroadcastOptions,boolean,boolean,int,int,int,int,int,BackgroundStartPrivileges,int[],BiFunction:
com.android.server.am.BroadcastQueue.enqueueBroadcastLocked::BroadcastRecord:
// 从这里开始,安卓U和U之前的代码出现不一样,这里只记录U之前的版本
com.android.server.am.BroadcastQueueImpl.enqueueBroadcastLocked::BroadcastRecord:
com.android.server.am.BroadcastQueueImpl.enqueueParallelBroadcastLocked::BroadcastRecord:
// 广播存在了这个变量里
//com.android.server.am.BroadcastQueueImpl.mParallelBroadcasts
// 广播入队后,发handler消息处理广播
com.android.server.am.BroadcastQueueImpl.enqueueBroadcastLocked::BroadcastRecord:
com.android.server.am.BroadcastQueueImpl.scheduleBroadcastsLocked:::
android.os.Handler.sendMessage::Message:
// handler 到主线程
// com.android.server.am.BroadcastQueueImpl.BROADCAST_INTENT_MSG
com.android.server.am.BroadcastQueueImpl.BroadcastHandler.handleMessage::Message:
com.android.server.am.BroadcastQueueImpl.processNextBroadcast::boolean:
com.android.server.am.BroadcastQueueImpl.processNextBroadcastLocked::boolean,boolean:
com.android.server.am.BroadcastQueueImpl.processCurBroadcastLocked::BroadcastRecord,ProcessRecord:
// binder 到接收广播的app进程
android.app.ActivityThread.ApplicationThread.scheduleReceiver::Intent,ActivityInfo,CompatibilityInfo,int,String,Bundle,boolean,boolean,int,int,int,String:
android.app.ActivityThread.sendMessage::int,Object:
// handler 到主线程
// android.app.ActivityThread.H.RECEIVER
android.app.ActivityThread.H.handleMessage::Message:
android.app.ActivityThread.handleReceiver::ActivityThread.ReceiverData:
android.content.BroadcastReceiver.onReceive::Context,Intent:

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android Studio 中调用相机并保存到相册,可以按照以下步骤进行操作: 1. 添加相机权限 在 AndroidManifest.xml 文件中添加相机权限: ```xml <uses-permission android:name="android.permission.CAMERA" /> ``` 2. 添加文件写入权限 在 AndroidManifest.xml 文件中添加文件写入权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 3. 创建一个 FileProvider 在 AndroidManifest.xml 文件中添加一个 FileProvider: ```xml <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ``` 4. 创建 file_paths.xml 文件 在 res/xml 目录下创建一个 file_paths.xml 文件,并添加以下内容: ```xml <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="Pictures" /> </paths> ``` 5. 创建一个 Intent 并启动相机 在你想要启动相机的地方,创建一个 Intent 并启动相机: ```java private static final int REQUEST_IMAGE_CAPTURE = 1; private String currentPhotoPath; private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File ex.printStackTrace(); } if (photoFile != null) { Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", photoFile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } } private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents currentPhotoPath = image.getAbsolutePath(); return image; } ``` 这个代码会启动相机,并将拍摄的照片保存到一个指定的文件中。 6. 处理结果 在 onActivityResult 方法中处理相机返回的结果: ```java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { // GalleryAddPic(currentPhotoPath); Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(currentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent); } } ``` 这个代码会将拍摄的照片添加到相册中。 注意:为了使照片可以被其他应用访问,你需要将照片添加到 MediaStore 中。可以使用以下代码将照片添加到 MediaStore 中: ```java private void GalleryAddPic(String currentPhotoPath) { Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(currentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值