Implementing activity how to use methods in jar in android
i am creating own framework api (telephone services related api) i am creating one library app this library app to create myjar.jar i am developing another application just i am adding asset folder this myjar.jar and configure this jar adding buildpath in coding importing the package
this application run into real device:
MacTesting mp = new MacTesting();
mp.getMacAddress();
Log.v("1111","this is mac add"+ mp.getMacAddress());
result is null
public class MacIdTesting extends Activity implements Parcelable
{
public static final String KEY_WIFI_MAC_ADDRESS = null;
public static final String READ_PHONE_STATE = null;
/** Called when the activity is first created. */
public String mMacAddress;
public String phonenumber;
void setMacAddress(String macAddress) {
this.mMacAddress = macAddress;
}
public String getMacAddress() {
return mMacAddress;
}
public String getLine1Number()
{
ContextWrapper mContext = null;
mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, "Requires READ_PHONE_STATE");
MacIdTesting mPhone = null;
return mPhone.getLine1Number();
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = manager.getCo开发者_如何学CnnectionInfo();
String MACAddress = wifiInfo.getMacAddress();
System.out.println("macsddress "+MACAddress);
}
@Override
public int describeContents()
{
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
// TODO Auto-generated method stub
dest.writeString(mMacAddress);
}
}
see this screenshot
how can solve this problem
Your class is an activity and has onCreate method, but not a constructor.
I change the onCreate method to be a constructor like that (and remove the extends activity
):
public MacIdTesting(Context ctx)
{
WifiManager manager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = manager.getConnectionInfo();
mMacAddress = wifiInfo.getMacAddress();
}
And call it from the activity like that:
MacTesting mp = new MacTesting(this);
Also, I would keep private
members instead of public
(private String mMacAddress
).
There are some issues:
MacIdTesting
is an Activity but you use it theAndroidMac
class which is an Activity by itself. Therefore the lifecycle methods ofMacIdTesting
will not get called. You can for exampleAndroidMac
let extendMacIdTesting
. Then you could callgetMacAddress()
.The code in
MacIdTesting
is faulty: You are assigning the MAC address to some local variable and the field in the object never gets set.Before writing a library I would suggest to read more about the android lifecycle and possibly about Java.
add
this.mMacAddress = MACAddress;
as the last line to the onCreate() method of MacTesting.
精彩评论