mercredi 7 mai 2014

Débuter avec le TelephonyManager et l'ActivityManager sur Androïd

package com.reunisoft.gsmhack.gsmhack;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.CellIdentityGsm;
import android.telephony.CellInfo;
import android.telephony.CellInfoGsm;
import android.telephony.CellLocation;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

// A DVA FREE OPEN SOURCE DEMO
public class MainActivity extends ActionBarActivity {

    int i = 0;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        refreshAllInfos();

        final Button button = (Button)findViewById(R.id.button);
        button.setText("Refresh");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshAllInfos();
            }
        });
    }

    public void refreshAllInfos() {
        try {
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            String str = "IMEI=" + telephonyManager.getDeviceId() + "\r\n";
            str += "Line 1 number=" + telephonyManager.getLine1Number() + "\r\n";
            str += "Network Op = " + telephonyManager.getNetworkOperatorName() + "\r\n";

            boolean roaming = telephonyManager.isNetworkRoaming();
            str += "Roaming = " + String.valueOf(roaming) + "\r\n";
            str += "Soft version = " + telephonyManager.getDeviceSoftwareVersion() + "\r\n";

            final GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
            str += "Cell ID = " + cellLocation.getCid() + "\r\nActive processes ID = ";

            ActivityManager manager =  (ActivityManager)getSystemService(ACTIVITY_SERVICE);
            final List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = manager.getRunningAppProcesses();
            for(int i=0;i<runningAppProcesses.size();i++){
                ActivityManager.RunningAppProcessInfo rapi = (ActivityManager.RunningAppProcessInfo) runningAppProcesses.get(i);
                str += rapi.pid + "/";
            }
          
            updateTextView(str);
        }
        catch (Exception e){
            updateTextView(e.getMessage());
        }

    }

    public void updateTextView(String toThis) {
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(toThis);
        return;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /* Called when the application is minimized */
    @Override
    protected void onPause()
    {
        super.onPause();
    }

    /* Called when the application resumes */
    @Override
    protected void onResume()
    {
        super.onResume();
    }

}