Auto run code when Android starts up? - android

I want my code will run automatically when the android starts up.And,i know that the init.rc srcipt will be runned when the android starts up.So,i decided to add my code's information as the service item in the init.rc.But unfortunately, it fails.
Below are the steps that I used:
1. I build the server.c via the ndk's cross-compiler:
server.c:
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
void sys_log(char* msg) {
FILE *fp;
time_t t;
if((fp=fopen("/data/server.log","a")) >=0)
{
t=time(0);
fprintf(fp,"%s at time: %s\n", msg, asctime(localtime(&t)) );
}
fclose(fp);
}
int main() {
printf("server begin!");
sys_log("server begin!");
)
I add the following lines to the end of /init.rc:
service socketserver /data/server
oneshot
I restart the android device
I use the instruction cat /data/server.log to check the log, but there is nothing in the server.log!
I run the server directly. Everything runs okay. Both the log and the console will prompt what i want!
So in conclusion, I think the init.rc does not start the server that I built.
Are my steps correct? Any ideas? Thank you for your help in advance!

Related

How to find out the full directory of a file in Android phone to be used with fstream?

I am trying to test a very basic c++ code in CppDroid app to create a text file on my Android phone (Huawei P20pro). But it doesn't seem to work. I think the problem is with the file directory. Can someone let me know how to get the real full directory of a file/folder in Android phone?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream NewFile;
NewFile.open("/Internal Storage/CppDroid/projects/project_dec23a/Gyrocopter.txt");
NewFile << "This is a file about a gyrocopter. \n";
NewFile.close();
return 0;
}

How to print on android emulator console by using C coding in JNA framework

I am new to the android studio, could you please help me, I tried multiple time to print Logcats on Android emulator but unable to see Logcats print.Is there any possibility to print on Android emulator console by using Native code prints and also How to print Logcats on android studio logcat window using normal C printf.
I tried this one,
#include <stdio.h>
#include <android/log.h>
#define LOGD(...) __android_log_print(int prio, const char *tag, const char *fmt, ...)
int main()
{
int a=23;
LOGD(ANDROID_LOG_DEBUG,"Print something %d",a);
return 0;
}

Compiling two files in CCTools Android (GCC Addon installed)

I have 3 files,
1) q5.c
2) q5.h
3) q5fun.c
The contents of each file is listed below,
q5.c
#include <stdio.h>
#include "q5.h"
int number;
main() {
number = 2;
printf("%d\n",fun(5));
printf("%d\n",number);
}
q5.h
extern int number;
int fun(int);
q5fun.c
#include "q5.h"
int fun(int x) {
return x + number++;
}
On linux, code works fine. I am trying to make my platform mobile on a ground vehicle and I wanted to do such computations on my phone.
I installed CCTools and the GCC add-on to it,
When I compile the main program "q5.c", the compiler throws up saying "undefined reference to 'fun'. What is the terminal command am I suppose to execute?
In unix platform I run "cc q5.c q5fun.c"
Under android platform, what am I suppose to run?
Okay seems I can compile and produce the output the way I want. Now the problem is that a.out file does not have execute permission. Any quick tips on how to solve?
--
Execution from sdcard disabled by default for non-rooted devices. Copy your file to terminal home directory (it defined as /data/data/com.pdaxrom.cctools/root/cctools/home), change permission and run it:
cp myprogram ~/
chmod 755 ~/myprogram
~/myprogram
Also, you can use Makefile to compile two or more sources files.

why lstat() not work in the fuse-mounted directory?

I have a directory mounted by fuse,and i am intented to use lstat() against this dirctory.But,when i run the following code,it just wait there and prompt nothing.
And by the way,i run the fuse in the android emulator.
the code is:
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!");
char *path="/data/pwrite/test_12/";
struct stat *stbuf;
int res=12;
res=lstat(path, stbuf);
printf("%d",res);
}
And,"/data/pwrite/test_12/" is the fuse-mounted directory.What's more,when i try another dicrtory that share the same parent directory but not mounted by fuse,like /data/pwrite/test_13/,it works!
so,i definely sure it is leaded by fuse.But,i'm even more confused that whether it is due to the conflict between fuse and android.
Any idea?thx
Try without the bugs and see if that works better.
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!\n");
const char *path="/data/pwrite/test_13/";
struct stat stbuf;
int res=12;
res=lstat(path, &stbuf);
printf("%d\n",res);
}
why it not work?because there runs several fuse-deamon currently.

Native Camera support in adb shell for OpenCV - android

With the new 2.3rc version of OpenCV, we are able to run executables that use OpenCV in adb shell without dealing with JNI interfaces for android. The new version also comes with prebuilt native camera support. (see http://opencv.willowgarage.com/wiki/Android2.3.0 for details ) I would like to know if it is possible to capture a preview frame from the camera using an executable that runs run in adb shell. Using the code snippet in the documentation I wrote the following simple code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
const char* message = "Capture a frame!";
const char* errorMessage = "Could not open the camera!";
int main(int argc, char* argv[])
{
// print message to console
printf("%s\n", message);
cv::VideoCapture capture(CV_CAP_ANDROID + 0);
//cv::VideoCapture capture(CV_CAP_ANDROID + 1);//front camera for Android 2.3.3 or newer
if( !capture.isOpened() )
{
printf("%s\n", errorMessage);
return 0;
}
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cv::Mat frame;
capture >> frame;
if( !frame.empty() ){
imwrite("/mnt/sdcard/CaptureFrame.png", frame);
}
return 0;
}
(The build scripts are similar to HelloAndroid sample application in the downloaded library)
It builds and runs in adb shell but I cannot capture frame and thus write to the image file. I checked the logcat output and it seems to load the libraries and opens the camera but then it gets stuck at "capture>>frame" step.
Any ideas?
Thanks
Zafer

Categories

Resources