Android,java发送intent,native c/c++发送intent, am命令行执行程序。

先来看看google如何表述的:

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

一个Intent就是要执行的操作的抽象描述,是Android里很重要的,完成Android各个组件之间的通讯。

例如:启动一个Activity.

Intent intent = new Intent();
intent.setClassName("com.android.browser.ProxySelector");
startActivity(intent);


Java发送一个Intent相信大家都很熟悉,那我们如何在Native层的代码里如何发送呢,比如在C中发送。其实Android提供了一个工具am,是命令行执行Android程序等。所以发送一个Intent就容易的多了:

char intent[1024] = {0};

sprintf(intent, "am start -a com.android.browser.ProxySelector);

system(intent);


顺便介绍下am:

EMMA: collecting runtime coverage data ...
usage: am [subcommand] [options]

    start an Activity: am start [-D] [-W] <INTENT>
        -D: enable debugging
        -W: wait for launch to complete

    start a Service: am startservice <INTENT>

    send a broadcast Intent: am broadcast <INTENT>

    start an Instrumentation: am instrument [flags] <COMPONENT>
        -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
        -e <NAME> <VALUE>: set argument <NAME> to <VALUE>
        -p <FILE>: write profiling data to <FILE>
        -w: wait for instrumentation to finish before returning

    start profiling: am profile <PROCESS> start <FILE>
    stop profiling: am profile <PROCESS> stop

    start monitoring: am monitor [--gdb <port>]
        --gdb: start gdbserv on the given port at crash/ANR

    <INTENT> specifications include these flags:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--esn <EXTRA_KEY> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [-n <COMPONENT>] [-f <FLAGS>]
        [--grant-read-uri-permission] [--grant-write-uri-permission]
        [--debug-log-resolution]
        [--activity-brought-to-front] [--activity-clear-top]
        [--activity-clear-when-task-reset] [--activity-exclude-from-recents]
        [--activity-launched-from-history] [--activity-multiple-task]
        [--activity-no-animation] [--activity-no-history]
        [--activity-no-user-action] [--activity-previous-is-top]
        [--activity-reorder-to-front] [--activity-reset-task-if-needed]
        [--activity-single-top]
        [--receiver-registered-only] [--receiver-replace-pending]
        [<URI>]

---

原来也是如此简单的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以使用AIDL(Android Interface Definition Language)来定义跨进程通信的接口。如果要让Java客户端调用本地实现的AIDL服务,可以使用JNI来实现。以下是实现步骤: 1.编写本地服务代码:使用C或C++编写本地服务的代码,并将其编译成本地库,例如.dll文件(Windows平台)或.so文件(Linux平台)。 2.编写AIDL接口:在Android中,使用AIDL定义接口。你需要编写一个AIDL文件来定义服务的接口。 3.编写Java代码:在Java中编写客户端代码,并使用JNI调用本地库。你需要将AIDL接口转换为Java接口,并使用JNI调用本地实现。 4.将本地库加载到Java中:使用System.loadLibrary()方法将本地库加载到Java中。 5.连接到服务:在Java代码中连接到AIDL服务。 以下是一个简单的示例代码: C/C++本地服务代码: ```c #include <jni.h> JNIEXPORT jstring JNICALL Java_com_example_MyService_nativeMethod(JNIEnv *env, jobject obj) { return (*env)->NewStringUTF(env, "Hello from native method!"); } ``` AIDL接口: ```aidl interface IMyService { String sayHello(); } ``` Java客户端代码: ```java public class MyService extends Service { // 定义本地方法 private native String nativeMethod(); // 实现AIDL接口 private final IMyService.Stub mBinder = new IMyService.Stub() { @Override public String sayHello() throws RemoteException { return nativeMethod(); // 调用本地方法 } }; @Override public IBinder onBind(Intent intent) { return mBinder; // 返回AIDL接口 } static { System.loadLibrary("mylib"); // 加载本地库 } } ``` 注意:这只是一个简单的示例,实际情况可能更加复杂。你需要仔细阅读JNI和AIDL文档,并且确保你的本地代码和Java代码之间的交互是正确的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值