2017-01-24 3 views
0

Ich versuche, eine Anwendung zu machen. Ich habe ein paar Probleme. Ich möchte Geräteinformationen in der Datei "DeviceActivity.java" erstellen und auf jeder Seite verwenden.Android Erstellen und Verwenden von Methoden

Der Codeblock I auf MainActivity.java erstellt:

public class MainActivity extends AppCompatActivity { 

String email; 
int MY_PERMISSIONS_EMAIL = 8889; 

String imei; 
int MY_PERMISSIONS_IMEI = 8889; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ////////////////////// E-Posta Sorgulama ////////////////////// 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(this, 
       new String[]{Manifest.permission.GET_ACCOUNTS}, 
       MY_PERMISSIONS_EMAIL); 
    } else { 
     Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ 
     Account[] accounts = AccountManager.get(MainActivity.this).getAccounts(); 
     for (Account account : accounts) { 
      if (emailPattern.matcher(account.name).matches()) { 
       email = account.name; 
      } 
     } 

    } 
    TextView epota = (TextView) findViewById(R.id.email); 
    epota.setText("E-posta: " + email); 
    ////////////////////// E-Posta Sorgulama Son ////////////////////// 

    ////////////////////// Cihaz Modeli Sorgulama ////////////////////// 
    String CihazModel = Build.MODEL; 
    TextView cihazmodel = (TextView) findViewById(R.id.cihaz_model); 
    cihazmodel.setText("Cihaz Modeli: " + CihazModel); 
    ////////////////////// Cihaz Modeli Sorgulama Son ////////////////////// 

    ////////////////////// Cihaz İsmi Sorgulama ////////////////////// 
    String CihazIsim = Build.MANUFACTURER; 
    TextView cihazisim = (TextView) findViewById(R.id.cihaz_isim); 
    cihazisim.setText("Cihaz İsmi: " + CihazIsim.toUpperCase()+ " " + CihazModel); 
    ////////////////////// Cihaz İsmi Sorgulama Son ////////////////////// 


    ////////////////////// Bellek Sorgulama ////////////////////// 
    RandomAccessFile reader = null; 
    String load = null; 
    DecimalFormat twoDecimalForm = new DecimalFormat("#.##"); 
    double totRam = 0; 
    String lastValue = ""; 
    try { 
     reader = new RandomAccessFile("/proc/meminfo", "r"); 
     load = reader.readLine(); 

     // Get the Number value from the string 
     Pattern p = Pattern.compile("(\\d+)"); 
     Matcher m = p.matcher(load); 
     String value = ""; 
     while (m.find()) { 
      value = m.group(1); 
      // System.out.println("Ram : " + value); 
     } 
     reader.close(); 

     totRam = Double.parseDouble(value); 
     // totRam = totRam/1024; 

     double mb = totRam/1024.0; 
     double gb = totRam/1048576.0; 
     double tb = totRam/1073741824.0; 

     if (tb > 1) { 
      lastValue = twoDecimalForm.format(tb).concat(" TB"); 
     } else if (gb > 1) { 
      lastValue = twoDecimalForm.format(gb).concat(" GB"); 
     } else if (mb > 1) { 
      lastValue = twoDecimalForm.format(mb).concat(" MB"); 
     } else { 
      lastValue = twoDecimalForm.format(totRam).concat(" KB"); 
     } 


    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     // Streams.close(reader); 
    } 


    String CihazBellek = lastValue; 
    TextView cihazbellek = (TextView) findViewById(R.id.cihaz_bellek); 
    cihazbellek.setText("Cihaz Bellek: " + CihazBellek); 
    ////////////////////// Bellek Sorgulama Son ////////////////////// 

    ////////////////////// İşletim Sistemi ////////////////////// 
    String CihazOs = "Android"; 
    TextView cihazos = (TextView) findViewById(R.id.cihaz_os); 
    cihazos.setText("Cihaz OS: " + CihazOs); 
    ////////////////////// İşletim Sistemi Son ////////////////////// 

    ////////////////////// Cihaz ID Sorgulama ////////////////////// 
    String CihazId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); 
    TextView cihazid = (TextView) findViewById(R.id.cihaz_id); 
    cihazid.setText("Cihaz ID: " + CihazId); 
    ////////////////////// Cihaz ID Sorgulama Son ////////////////////// 

    ////////////////////// Cihaz OS Versiyonu Sorgulama ////////////////////// 
    Integer CihazApi = Build.VERSION.SDK_INT; 
    TextView cihazapi = (TextView) findViewById(R.id.cihaz_api); 
    cihazapi.setText("Cihaz Api: " + CihazApi.toString()); 
    ////////////////////// Cihaz OS Versiyonu Sorgulama Son ////////////////////// 

    ////////////////////// Cihaz Zaman Dilimi Sorgulama ////////////////////// 
    String TimeZone = java.util.TimeZone.getDefault().getID(); 
    TextView timezone = (TextView) findViewById(R.id.timezone); 
    timezone.setText("Timezone: " + TimeZone); 
    ////////////////////// Cihaz Zaman Dilimi Sorgulama Son ////////////////////// 

    ////////////////////// Cihaz IMEI Sorgulama ////////////////////// 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(this, 
       new String[]{Manifest.permission.READ_PHONE_STATE}, 
       MY_PERMISSIONS_IMEI); 
    } else { 
     TelephonyManager tManager = (TelephonyManager) getBaseContext() 
       .getSystemService(Context.TELEPHONY_SERVICE); 
     imei = tManager.getDeviceId(); 

    } 

    TextView cihazimei = (TextView) findViewById(R.id.imei); 
    cihazimei.setText("Cihaz Imei: " + imei); 
    ////////////////////// Cihaz IMEI Sorgulama Son ////////////////////// 
} 
} 

Ich möchte die Codes auf dieser Seite passen auf anderen Seiten zu „DeviceActivity.java“ Seite und verwenden.

+0

und was ist ein Problem? Warum kannst du es nicht anpassen? Oder willst du einfach, dass jemand es an deiner Stelle tut? –

+0

Es funktioniert, wenn ich es der Homepage hinzufüge, aber ich kann es nicht auf einer anderen Seite ausführen. Ich wollte nicht, dass jemand anderes es macht, ich bat um Hilfe. @VladMatvienko –

+0

Warum können Sie es nicht von einer anderen Seite ausführen? Was ist ein Problem dort? Welche Art von Hilfe willst du? –

Antwort

0

Wenn ich richtig verstehe, einfach eine neue öffentliche Java-Klasse erstellen und diesen Code dort hinzufügen. Rufen Sie dann die Methode aus der Klasse auf der gewünschten Seite auf.

public class DeviceActivity { 
public DeviceActivity(Context context) {this.context = context;} 
public YourMethod(int someVariable, string someOtherVariable) { 
// Your code here. 
} 
} 

Dann nennen es wie folgt:

DeviceActivity deviceActivity = new DeviceActivity(YourActivity.this); 
deviceActivity.YourMethod(someVariable, someOtherVariable); 

this helps :)

EDIT

Nach dem, was ich von Ihnen zu verstehen, versuchen Sie die folgenden. Ich bin mir nicht sicher, welche Fehler Sie erleben, aber ich kann das Problem nicht klar verstehen. Folge dem untenstehenden Code.

public class MainActivity extends AppCompatActivity { 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     DatabaseActivity db = new DatabaseActivity(); 
     db.SaveInformationToDatabase(variable, otherVariable); //Add your own variables. 
    } 
} 

public class DatabaseActivity { 
    public void SaveInformationToDatabase(int variable, string otherVariable) { 
     //Add your code to save details to database. 
    } 

    public Request retrieveDetailsFromDatabase() { 
     //Add your code to retrieve the details from database as Json 
    } 
} 

public class DeviceActivity extends AppCompatActivity { 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     DatabaseActivity db = new DatabaseActivity(); 
     Request request = db.retrieveDetailsFromDatabase(); 

     //Use request to retrieve Json and set details retrieved. 
} 
+0

Leider hat es nicht funktioniert :( –

+0

Was genau versuchen Sie zu erreichen? Nach was ich verstanden habe, möchten Sie diesen Code von jeder Aktivität aufgerufen werden, ohne dass Sie den Code in jeder Activity-Klasse neu schreiben müssen? –

+0

Ich versuche es Erstellen von 3 Seiten 1- MainActivity.java: Sendet die Benutzerinformationen an die automatische Datenbank, wenn die Seite geöffnet wird 2- DatabaseActivity.java: Die Seite, die Informationen an die Datenbank sendet 3- DeviceActivity.java: Findet Die Geräteinformationen werden angezeigt und auf der DatabaseActivity.java-Seite angezeigt –

Verwandte Themen