获取IMEI号码时Android应用程序崩溃

时间:2014-01-29 06:09:06

标签: java android telephonymanager

我有以下类,只设置为获取设备ID。

package com.example.bootstart;


import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;

public class IMEI extends Activity {
    public String get_dev_id(){

        //Getting the Object of TelephonyManager 
        TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        //Getting IMEI Number of Devide
        String Imei=tManager.getDeviceId();

        return Imei;
    }
}

我的清单中设置了以下权限。

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

我尝试通过

从另一个类访问get_dev_id函数
package com.example.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.example.bootstart.IMEI;

import android.os.AsyncTask;
import android.util.Log;

public class AsyncSendData extends AsyncTask<String, Void, Boolean> {

    private static final String LOGTAG = "SENDSERVER";

    @Override
    protected Boolean doInBackground(String... params) {
        Log.i(LOGTAG, "FUNction Called");
        String url = params[0]; // URL.
        String lati = params[1];
        String longi = params[2];
        String welawa = params[3];

        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpPost httpPost = new HttpPost(url);
//              "http://www.domain.com/nv/maps/track_post");

        // Building post parameters
        // key and value pair
        Log.i(LOGTAG, "Before Imei");
        IMEI imei = new IMEI();
        String imei_num = imei.get_dev_id(); //***************IMEI Called Here****
        Log.i(LOGTAG, "After Imei");

        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
        nameValuePair.add(new BasicNameValuePair("imei", imei_num));
        nameValuePair.add(new BasicNameValuePair("welawa", welawa));
        nameValuePair.add(new BasicNameValuePair("lat", lati));
        nameValuePair.add(new BasicNameValuePair("long", longi));
        Log.i(LOGTAG, "After Name Value");
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            Log.i(LOGTAG, "After Response");
            HttpResponse response = httpClient.execute(httpPost);
            Log.i(LOGTAG, "Response " + response);
            if (response != null) {
                return response.getStatusLine().getStatusCode() == 200;
            }
        } catch (Exception e) {
            //
            Log.e(LOGTAG, e.getMessage());
        }
        return false;
    }

}

在“Log.i(LOGTAG,”Imei之前“)之后,日志没有输出任何内容;”。 对于可能发生的事情有任何想法吗?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

//您需要将IMEI活动更改为类

public static class IMEI {

    public static String get_dev_id(Context ctx){

        //Getting the Object of TelephonyManager 
        TelephonyManager tManager = (TelephonyManager)ctx.getSystemService(Context.TELEPHONY_SERVICE);

        //Getting IMEI Number of Devide
        String Imei=tManager.getDeviceId();

        return Imei;
    }
}

//你需要用

来调用它
String imei_num = IMEI.get_dev_id(context);

<强>编辑:

您可以使用静态代替..