make getSystemService() recognize our new system service - android

I'm using the book "Embedded Android".
I'm making a new System Service using AOSP(4.0.3_r1).
I want my system service to be registered in frameworks/base/core/java/android/content/app/ContextImpl.java so that I can use it through getSystemService() method.
The problem is, I can't find the app folder under content:androidroot/frameworks/base/core/java/android/content/app/ContextImpl.java
But, I found it in:androidroot/frameworks/base/core/java/android/app/ContextImpl.java
Are these 2 files the same? or is it just missing(the content/app folder)?
Any idea on what to do?

Karim wrote his book mostly orienting on Android 2.3.4 version. Something can be changed from this time. This is an example what has been changed.
Are these 2 files the same? or is it just missing(the content/app folder)?
These are the same files.
Any idea on what to do?
As I said the implementation has been changed. I looked into the code and here what you can change to make your code working (I can only suppose because I did not actually build my code). In the static block of ContextImpl class you need to add the following code:
registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(OPERSYS_SERVICE);
IOpersysService service = IOpersysService.Stub.asInterface(b);
return new OpersysManager(service);
}});

You need to use SystemServer which holds all system services' names.
You should check this link out:
http://processors.wiki.ti.com/index.php/Android-Adding_SystemService

Related

How to create TelephonyDisplayInfo object in android R device

I want to get getOverrideNetworkType() in android Like as Android CA 5G NSA NR but Enable to Create TelephonyDisplayInfo object to get this. I am using this code but get
TelephonyDisplayInfo mTelephonyDisplayInfo;
mTelephonyDisplayInfo = new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_UNKNOWN,
TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE);
but get error:-
TelephonyDisplayInfo() is not public in android.telephony.TelephonyDisplayInfo. Cannot be accessed from outside package this error.
Comment from Google support:
Although the constructor is public, it's #hide and not a part of the
API surface, so it shouldn't be accessible to developers. To get the
TelephonyDisplayInfo, please use
PhoneStateListener#onDisplayInfoChanged instead. Closing this as
working as intended.

"Firebase App named '[DEFAULT]' already exists."

So I was trying to use Firebase in an Android service which is in a seperate process and this error came up while doing:
FirebaseDatabase.getInstance()....
or
FirebaseAuth.getInstance()....
I wrote this check snippet to overcome the problem:
boolean hasBeenInitialized=false;
List<FirebaseApp> firebaseApps = FirebaseApp.getApps(this);
for(FirebaseApp app : firebaseApps){
if(app.getName().equals("[DEFAULT]")){
hasBeenInitialized=true;
}
}
if(!hasBeenInitialized) {
FirebaseApp.initializeApp(this, FirebaseOptions.fromResource(this));
}
// Only then getInstance() will work
And IT WORKS pretty good BUT I'm not sure this is the right way to do it (it's the hardcoded [DEFAULT] that bothers me...).
Does anyone knows a proper way to do this ?
You can compare to FirebaseApp.DEFAULT_APP_NAME instead. See the reference docs.

cn1 - download file to phone's download directory

I'm trying to allow an app to download files to the public 'Downloads' directory so it's available on the device in a generic fashion. I succeeded in downloading the files in the simulator to the .cn1 directory on my computer, but I couldn't find a straightforward way to get the file into a public directory on the device. I really expected there to be a method in the FileSystemStorage class that would allow this, but none of them seem to be what I'm looking for.
So I tried writing a simple native bridge to get the path to the public directory, starting with Android. I have a very simple class that looks like this:
public class DownloadDirectoryImpl {
public static String getDownloadDirectory(){
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
}
public boolean isSupported(){
return true;
}
}
The app compiles fine, but when I click on the file to download it, I see the same line repeating itself in the logcat a few thousand times:
W/System.err: at net.gesher.downloadDirectory.NativeDownloadDirectoryImpl.getDeviceDownloadDirectory(NativeDownloadDirectoryImpl.java:20)
But it doesn't state what the error is.
So, I'm looking for advice either a) to improve my android code so that it works, or b) the cn1 proper way of getting this directory path.
Thanks a ton!
If you have a lot of lines saying at... and all refer to the same method then you have a recursive call leading to a stack overflow. Since the only method you mentioned is your native method I'm assuming you called your own method within the native implementation and got into a recursive loop.

Detect if an android app is running on background

I want to check if my app is running on a background mode.
The problem is that i have many activities(list activities, map activities etc.). Initially I have tried in the life cycle's resume and pause(or the onUserLeaveHint) methods to set a static boolean as true or false and work with this way. But this obviously can't work because when I move from one activity to another, the previous one get paused.
Also, I've read here on stackoverflow that the getRunningTasks() should be used only for debugging purposes. I did a huge research but I can't find a solution. All I want to do is to be able to detect if a the app is running on a background. Can anyone propose me a way, or express any thought on how can I do that?
You can try the same mechanism (a boolean attribute) but on application side rather than activity side. Create a class which extends Application, declare it in the manifest file under <application android:name=YourClassApp>.
EDIT: I assume you know that activities aren't intended for background processing, if not you should take a look at the Services.
I don't know if this will help but you can use
getApplicaton().registerActivityLifecycleCallbacks(yourClass);
To get a birds eye view of how your activities are displayed in the FG. (For older s/w you can use this)
If your Application has a Service you could have a static get/set which accesses a static variable. Do not do this in Activities though, it causes mem leaks.
But realistically speaking there is no tidy way of tracking if your application is running or not.
I had the same problemen when overwriting the Firebase push messaging default behavior (show notifications only when in the background) I checked how Firebase did this by looking in the .class file com.google.firebase.messaging.zzb:53 (firebase-messaging:19.0.1) which appears to us getRunningAppProcesses. Mind you FireBase is created by Google them self. So I'm assuming it's pretty save to use. Cleaned up version:
List<ActivityManager.RunningAppProcessInfo> runningApps;
boolean isInForeground =false;
if ((runningApps = ((ActivityManager)this.getApplication().getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses()) != null) {
Iterator runningApp = runningApps.iterator();
int myPid = Process.myPid();
while(runningApp.hasNext()) {
ActivityManager.RunningAppProcessInfo processInfo;
if ((processInfo = (ActivityManager.RunningAppProcessInfo)runningApp.next()).pid == myPid) {
isInForeground = processInfo.importance == 100;
break;
}
}
}

Call APNDroid from my app

I am trying to write an app that calls APNDroid for enabling/disabling the 3G.
I looked at the code snippets provided by the developer.
I can't seem to understand how to incorporate them in my code.
Here is the code snippet I am referring to:
boolean targetState = getCheckBoxState(R.id.target_state);
boolean keepMms = getCheckBoxState(R.id.keep_mms);
boolean showNotification = getCheckBoxState(R.id.show_notification);
Intent intent = new Intent(ApplicationConstants.CHANGE_STATUS_REQUEST);
int onState = ApplicationConstants.State.ON;
int offState = ApplicationConstants.State.OFF;
intent.putExtra(ApplicationConstants.TARGET_MMS_STATE, keepMms ? onState : offState);
intent.putExtra(ApplicationConstants.TARGET_APN_STATE, targetState ? onState : offState);
intent.putExtra(ApplicationConstants.SHOW_NOTIFICATION, showNotification);
MyActivity.this.startActivityForResult(intent, CHANGE_REQUEST);
Specifically, I do not understand how this code knows to call an APNDroid activity.
The constants are found here
Obviously none of this is recognized by "eclipse" when I embed it in my code.
Should I import something?
Should I somehow give a reference to the APNDroid app?
Any help will be much appreciated.
Thank you.
Avi
You first have to checkout the project. Following these steps
You also have in that piece of code the name of the Intent to call. Have a look here to see how to call the intent of another applications in Android
Call or start external Application Android

Categories

Resources