Wednesday, 19 December 2012
Little Green Robot Developer: Share data between two Android applications by usi...
Little Green Robot Developer: Share data between two Android applications by usi...: APP1 package com.example.app1; import android.app.Activity; import android.content.ComponentName; import android.content.Context; i...
Share data between two Android applications by using SharedPreferences
APP1
package com.example.app1;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
dataWriter();
i.setClassName("com.example.app2", "com.example.app2.MainActivity");
Intent intentDeviceTest = new Intent("android.intent.action.MAIN");
intentDeviceTest.setComponent(new ComponentName("com.example.app1",
"com.example.app2.MainActivity"));
startActivity(i);
}
});
public void dataWriter(){
String strShareValue = "Hello! this is shared data";
SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();
}
APP2
package com.example.app2;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
String dataShared;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataRead();
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(dataShared);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void dataRead(){
Context con;
try {
con = createPackageContext("com.example.app1", 0);
SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
dataShared = pref.getString("demostring", "No Value");
}
catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}
}
}
package com.example.app1;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
dataWriter();
i.setClassName("com.example.app2", "com.example.app2.MainActivity");
Intent intentDeviceTest = new Intent("android.intent.action.MAIN");
intentDeviceTest.setComponent(new ComponentName("com.example.app1",
"com.example.app2.MainActivity"));
startActivity(i);
}
});
public void dataWriter(){
String strShareValue = "Hello! this is shared data";
SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();
}
APP2
package com.example.app2;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
String dataShared;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataRead();
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(dataShared);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void dataRead(){
Context con;
try {
con = createPackageContext("com.example.app1", 0);
SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
dataShared = pref.getString("demostring", "No Value");
}
catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}
}
}
Thursday, 22 November 2012
Call Other application Activity inside Your Application via Intent in Android
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClassName("PackageNameOfOtherApp", "ClassNameWithPackageOfOtherApp");
Bundle b = new Bundle();
b.putString("name", "umer");
b.putString("num", "1234");
i.putExtras(b);
startActivity(i);
}
});
**************Menifiest**************
</activity>
<activity android:name="ClassNameWithPackageOfOtherApp" >
</activity>
latitude and longitude from GPS and network android
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, (float) 0, this);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_HIGH);
criteria.setAccuracy(Criteria.ACCURACY_MEDIUM);
String bestprovider = locationManager.getBestProvider(criteria, false);
Location lastknownlocation = locationManager.getLastKnownLocation("gps");
//Toast.makeText(this, GlobalInfo.Latitude+"test", Toast.LENGTH_SHORT).show();
if(lastknownlocation!=null)
{
lat=lastknownlocation.getLatitude();
lng=lastknownlocation.getLongitude();
Toast.makeText(this, lat+".. from gps .."+ lng, Toast.LENGTH_LONG).show();
}else{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, (float) 0, this);
lastknownlocation = locationManager.getLastKnownLocation("netwotk");
if(lastknownlocation!=null)
{
lat=lastknownlocation.getLatitude();
lng=lastknownlocation.getLongitude();
Toast.makeText(this, lat+".. from sim .."+ lng, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
lat=location.getLatitude();
lng=location.getLongitude();
Log.d("GPS", "onLocationChanged");
Toast.makeText(this,"lat::" +lat+"::long::"+lng, Toast.LENGTH_SHORT).show();
tv.setText("Latitude:: "+ lat + ":: longitude:: "+ lng);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
Tuesday, 11 September 2012
Sim info android source code
package com.umer.cellid;
import java.util.Locale;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OpencellidActivity extends Activity {
String latitude;
String longitude;
public class OpenCellID {
String mcc; //Mobile Country Code
String mnc; //mobile network code
String cellid; //Cell ID
String lac; //Location Area Code
Boolean error;
String strURLSent;
String GetOpenCellID_fullresult;
public Boolean isError(){
return error;
}
public void setMcc(String value){
mcc = value;
}
public void setMnc(String value){
mnc = value;
}
public void setCallID(int value){
cellid = String.valueOf(value);
}
public void setCallLac(int value){
lac = String.valueOf(value);
}
public String getLocation(){
return("latitude: "+latitude + " longitude: " + longitude);
}
public void groupURLSent(){
strURLSent =
"http://www.opencellid.org/cell/get?mcc=" + mcc
+"&mnc=" + mnc
+"&cellid=" + cellid
+"&lac=" + lac
+"&fmt=txt";
}
public String getstrURLSent(){
return strURLSent;
}
public String getGetOpenCellID_fullresult(){
return GetOpenCellID_fullresult;
}
public void GetOpenCellID() throws Exception {
groupURLSent();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(strURLSent);
HttpResponse response = client.execute(request);
GetOpenCellID_fullresult = EntityUtils.toString(response.getEntity());
spliteResult();
}
private void spliteResult(){
if(GetOpenCellID_fullresult.equalsIgnoreCase("err")){
error = true;
}else{
error = false;
String[] tResult = GetOpenCellID_fullresult.split(",");
latitude = tResult[0];
longitude = tResult[1];
}
}
}
int myLatitude, myLongitude;
OpenCellID openCellID;
TextView textSignal,batteryLevel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SignalStrengthListener signalStrengthListener;
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textMCC = (TextView)findViewById(R.id.mcc);
TextView textMNC = (TextView)findViewById(R.id.mnc);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);
TextView textGeo = (TextView)findViewById(R.id.geo);
TextView textRemark = (TextView)findViewById(R.id.remark);
TextView textImei = (TextView)findViewById(R.id.imei);
TextView textImsi = (TextView)findViewById(R.id.imsi);
TextView textGps = (TextView)findViewById(R.id.gps_langlat);
textSignal = (TextView)findViewById(R.id.signal);
batteryLevel=(TextView) findViewById(R.id.betry);
Button mapButton=(Button) findViewById(R.id.button1);
//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
String networkOperator = telephonyManager.getNetworkOperator();
String mcc = networkOperator.substring(0, 3);
String mnc = networkOperator.substring(3);
textMCC.setText("mcc: " + mcc);
textMNC.setText("mnc: " + mnc);
textImei.setText("Imei: " + getimeinum().toString());
textImsi.setText("imsi: " + getimsinum().toString());
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));
signalStrengthListener = new SignalStrengthListener();
((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener,SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String bestprovider = locationManager.getBestProvider(criteria, false);
Location lastknownlocation = locationManager.getLastKnownLocation(bestprovider);
if(lastknownlocation!=null)
{
textGps.setText("GPSLocation: Latitude: " + lastknownlocation.getLatitude()+", Longitude: "+ lastknownlocation.getLongitude());
}
textGeo.setText("APN: " + getapn().toString());
this.registerReceiver(this.myBatteryReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
openCellID = new OpenCellID();
openCellID.setMcc(mcc);
openCellID.setMnc(mnc);
openCellID.setCallID(cid);
openCellID.setCallLac(lac);
try {
openCellID.GetOpenCellID();
if(!openCellID.isError()){
// textGeo.setText(openCellID.getLocation());
textRemark.setText( "\n\n"
+ "URL sent: \n" + openCellID.getstrURLSent() + "\n\n"
+ "response: \n" + openCellID.getLocation()+ "\n\n");
}else{
textGeo.setText("Error");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
textGeo.setText("Exception: " + e.toString());
}
mapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(OpencellidActivity.this, Map.class);
Bundle b=new Bundle();
b.putString("latitude", latitude);
b.putString("longitude", longitude);
in.putExtras(b);
startActivity(in);
}
});
}
private BroadcastReceiver myBatteryReceiver
= new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
batteryLevel.setText("Battery Level: "
+ String.valueOf(arg1.getIntExtra("level", 0)) + "%");
}
}
};
public String getimeinum() {
String deviceID = null;
String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
CellLocation re= m_telephonyManager.getCellLocation();
GsmCellLocation loc = (GsmCellLocation)m_telephonyManager.getCellLocation();
String Cell_ID = String.format("%08x ", loc.getCid());
int deviceType = m_telephonyManager.getPhoneType();
switch (deviceType) {
case (TelephonyManager.PHONE_TYPE_GSM):
break;
case (TelephonyManager.PHONE_TYPE_CDMA):
break;
case (TelephonyManager.PHONE_TYPE_NONE):
break;
default:
break;
}
// String imsi = m_telephonyManager.getSubscriberId();
deviceID = m_telephonyManager.getDeviceId();
return deviceID ;
}
public String getimsinum()
{
String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
String imsi = m_telephonyManager.getSubscriberId();
return imsi;
}
private class SignalStrengthListener extends PhoneStateListener
{
@Override
public void onSignalStrengthsChanged(android.telephony.SignalStrength signalStrength) {
// get the signal strength (a value between 0 and 31)
int strengthAmplitude = signalStrength.getGsmSignalStrength();
//do something with it (in this case we update a text view)
textSignal.setText(" signal strength: "+String.valueOf(strengthAmplitude));
// signal=String.valueOf(strengthAmplitude);
super.onSignalStrengthsChanged(signalStrength);
}
}
public String getLocation()
{
String name = null;
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String bestprovider = locationManager.getBestProvider(criteria, false);
Location lastknownlocation = locationManager.getLastKnownLocation(bestprovider);
Geocoder geocoder = new Geocoder(OpencellidActivity.this, Locale.getDefault());
if(lastknownlocation!=null)
{
lastknownlocation.getLatitude();
lastknownlocation.getLongitude();
}
return name;
}
public String getapn()
{
final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
//receiving cursor to preffered APN table
Cursor c = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
//moving the cursor to beggining of the table
c.moveToFirst();
//now the cursor points to the first preffered APN and we can get some
//information about it
//for example first preffered APN id
int index = c.getColumnIndex("_id"); //getting index of required column
Short id = c.getShort(index); //getting APN's id from
//we can get APN name by the same way
index = c.getColumnIndex("name");
String name = c.getString(index);
return name;
}
}
Manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Monday, 3 September 2012
how to extract code of apk file.
step 1:Download dex2jar here. Create a new java project and paste (dex2jar-0.0.7.11-SNAPSHOT/lib ) jar files .Copy apk file into newly created java projectRun it and after refresh the project ,you get jar file .Using java decompiler you can view all java class files
step 2: Download java decompiler here
One more way
- Download Dex2Jar zip from this link :http://code.google.com/p/dex2jar/
- Unzip the downloaded zip file.
- Open command prompt & write the following command on reaching to directry where dex2jar exe is there and also copy the apk in same directory.dex2jar targetapp.apk file(./dex2jar app.apk on terminal)
- http://java.decompiler.free.fr/?q=jdgui download decompiler from this link.
- Open ‘targetapp.apk.dex2jar.jar’ with jd-gui File > Save All Sources to sava the class files in jar to java files.
Subscribe to:
Posts (Atom)