我试图提出申请。我有一些问题。 我想在" DeviceActivity.java"中创建设备信息。文件并在任何页面上使用它。
我在MainActivity.java上创建的代码块:
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 //////////////////////
}
}
我想将此页面上的代码改编为" DeviceActivity.java"页面并在其他页面上使用它。
答案 0 :(得分:0)
如果我理解正确,只需创建一个新的公共java类并在那里添加这些代码。然后从所需页面上的类调用该方法。
public class DeviceActivity {
public DeviceActivity(Context context) {this.context = context;}
public YourMethod(int someVariable, string someOtherVariable) {
// Your code here.
}
}
然后按如下方式调用:
DeviceActivity deviceActivity = new DeviceActivity(YourActivity.this);
deviceActivity.YourMethod(someVariable, someOtherVariable);
希望这会有所帮助:)
修改强>
根据我的理解,请尝试以下操作。我不确定你遇到了什么错误,但我似乎无法清楚地理解这个问题。请遵循以下代码。
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.
}