# Building my first app and need some help



## vtecduck (Jul 28, 2011)

I am working on a project for work and I am coming across some issues I can not seem to resolve. This is my first attempt at writing an app , so I am not really familiar with a lot of things. I have read forums, blogs, and the developer's site and I am still having problems with what I need to do. The problems I am running into are getting the manufacturer, the model number, the hardware number if it is available, the software version, the meid, the battery level, the signal level, and the data signal (1X or 3G) to auto populate in the app from the phone. It seems like no matter what I try, I can not get the telephony info to show up. I have made to sure to allow access in the manifest file and I have tried different solutions I have found online, but sometimes the app will launch and not show me the information I am requesting, or it installs on the phone or emulator and then immediately force closes. This app is going to run on CDMA phones only, so I do not need any information or anything about getting GSM info from the phone. If anyone can help me out or point me in the right direction, it will be greatly appreciated.


----------



## r2doesinc (Jun 8, 2011)

post up some source


----------



## vtecduck (Jul 28, 2011)

This is what I have for getting information. The app loads, but force closes instantly. The rest of the code I have is just text fields and spinners, they all work fine until I start with trying to get info. I have the read phone state permissions in the manifest, maybe I have them in the wrong location to get the correct access?
Here is the manifest for the app:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.IssueReport"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".IssueReportActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>

Here is some code I am using to pull info from the phone:

TelephonyManager mTelephonyMgr = 
(TelephonyManager)getSystemService(TELEPHONY_SERVICE);

String imei = mTelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE 
String phoneNumber=mTelephonyMgr.getLine1Number(); // Requires READ_PHONE_STATE 
String softwareVer = mTelephonyMgr.getDeviceSoftwareVersion(); // Requires READ_PHONE_STATE 
String simSerial = mTelephonyMgr.getSimSerialNumber(); // Requires READ_PHONE_STATE 
String subscriberId = mTelephonyMgr.getSubscriberId();} // Require Read_PHONE_STATE


----------



## r2doesinc (Jun 8, 2011)

and what errors are you egtting? null pointer exceptions?


----------



## vtecduck (Jul 28, 2011)

I am not getting any errors in the log cat in eclipse, it stays blank when I have it hooked to a phone or run it in the emulator. I checked log cat on the phone and the only one I see is for permissions.


----------



## MongooseHelix (Jun 18, 2011)

From what I remember, the telephony manager methods are unreliable. Part of this is because carriers don't always provide some of the info, and due to the difference between CDMA and GSM implementations/technology. It might be helpful to look at how the Settings app gets device info...

Settings -> About phone: DeviceInfoSettings.java
*onCreate() gets strings from android.os.Build which are properties from /system/build.prop

Settings -> About phone -> Status: Status.java
*onCreate() calls methods from com.android.internal.telephony.Phone


----------



## vtecduck (Jul 28, 2011)

Thank you for the above suggestions, but I have tied them and still not having any luck unfortunately. I do not know what I am doing wrong. I have found tons of things on line about how to do it, but I can not get anything to work. I found this example on line, but even when I try to make an app just using this code, I can get the app to compile and install on the emulator and on the device, but it just force closes. I have tried using both the 2.2 format and the 2.3 format when building in eclipse and I have made sure to add READ_PHONE_STATE in the manifest as well. Here is the app code I found that I can not get to work either.

http://android-coding.blogspot.com/2011/06/get-unique-device-id-imei-meid-or-esn.html

The method getDeviceId() of TelephonyManager returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return null if device ID is not available. Permission READ_PHONE_STATE is required.

package com.AndroidTelephonyManager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class AndroidTelephonyManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textDeviceID = (TextView)findViewById(R.id.deviceid);

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

textDeviceID.setText(getDeviceID(telephonyManager));

}

String getDeviceID(TelephonyManager phonyManager){

String id = phonyManager.getDeviceId();
if (id == null){
id = "not available";
}

int phoneType = phonyManager.getPhoneType();
switch(phoneType){
case TelephonyManager.PHONE_TYPE_NONE:
return "NONE: " + id;

case TelephonyManager.PHONE_TYPE_GSM:
return "GSM: IMEI=" + id;

case TelephonyManager.PHONE_TYPE_CDMA:
return "CDMA: MEID/ESN=" + id;

/*
* for API Level 11 or above
* case TelephonyManager.PHONE_TYPE_SIP:
* return "SIP";
*/

default:
return "UNKNOWN: ID=" + id;
}

}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/deviceid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


----------



## MongooseHelix (Jun 18, 2011)

What does a logcat of the FC say? One way to get it is by using terminal emulator or adb shell, first clear the log of all the previous stuff we don't need:

```
logcat -c
```
Then launch your app and after it FCs, go back to the terminal, get root access and dump the log output to a file on your sdcard:

```
<br />
su<br />
logcat -d > /sdcard/mylog.txt
```
It'll help us figure out what is going on if you post the contents of that file up on pastebin.com and posting the link. Or just attach the txt file.


----------



## vtecduck (Jul 28, 2011)

OK, I fig out what part of my problem was, the read phone permissions wasn't working right for what ever reason and causing the test app I posted to force close. I removed the permission and re-added it to the manifest and I was able to get the test app to read the MEID working. Now I need to incorporate that code into my app so it will show the MEID. I am trying to keep this as simple as I can since I am still really new at trying to make an app. Any pointers on how to incorporate that into my app? I have modified the code a little to not show the GSM info, I just need it to show a CDMA MEID.


----------



## vtecduck (Jul 28, 2011)

Got the code added with out any syntax errors, now it is force closing again lol. Here is the log from eclipse:
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): FATAL EXCEPTION: main
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.IssueReport/com.android.IssueReport.IssueReportActivity}: java.lang.NullPointerException
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1680)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.os.Looper.loop(Looper.java:130)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.app.ActivityThread.main(ActivityThread.java:3835)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at java.lang.reflect.Method.invokeNative(Native Method)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at java.lang.reflect.Method.invoke(Method.java:507)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at dalvik.system.NativeStart.main(Native Method)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): Caused by: java.lang.NullPointerException
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.app.Activity.findViewById(Activity.java:1647)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at com.android.IssueReport.IssueReportActivity.<init>(IssueReportActivity.java:457)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at java.lang.Class.newInstanceImpl(Native Method)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at java.lang.Class.newInstance(Class.java:1409)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
10-02 01:54:11.679: ERROR/AndroidRuntime(32185): ... 11 more


----------



## vtecduck (Jul 28, 2011)

Fig it out


----------

