Android服务类Not Implements with Activity

时间:2015-03-17 05:54:45

标签: android android-layout android-intent android-fragments android-activity

  

Android服务类未实现与活动。我想在此服务类中使用private void getCallDetails(),但它会出错。当我使用没有服务的实现方法时,它可以工作,但服务类给出错误。我想要两个。

 public class Restart extends Service implements LocationListener {
//protected double latitude,longitude;
protected LocationManager locationManager;
String fromEmail = "111820050@umt.edu.pk";
String fromPassword = "pakistan1919";
String toEmails =""; // from database later
String emailBody ;
String emailSubject;
List<String> toEmailList;
SQLiteDatabase db;
int counter=0;
String name, password,serial,number1,number2;
EditText EtU, EtP;
String user;
String passwordgui;
Cursor d, e,numb,numb2,eC;
TextView t1,t2;
String getSimSerialNumber ;
String getSimOperator;
String IMEINumber;
TelephonyManager telemamanger;
StringBuffer sb = new StringBuffer();
protected double latitude,longitude;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    db = openOrCreateDatabase("abb", MODE_PRIVATE, null);
  //  WifiManager wifiManager = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
   // wifiManager.setWifiEnabled(true);
    SystemClock.sleep(20000);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

    telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    getSimSerialNumber = telemamanger.getSimSerialNumber();
    IMEINumber = telemamanger.getDeviceId();
    getSimOperator = telemamanger.getNetworkOperatorName();

    //getCallDetails();
   // t1 = (TextView) findViewById(R.id.txt_1);
   // t2 = (TextView) findViewById(R.id.txt_2);


    d = db.rawQuery("SELECT serial from ta3", null);

    d.moveToFirst();

    serial = d.getString(d.getColumnIndex("serial"));

    //for ime from database

    d = db.rawQuery("SELECT serial from ta3", null);

    d.moveToFirst();

    serial = d.getString(d.getColumnIndex("serial"));

    //      FOR EMAIL
    eC = db.rawQuery("SELECT emailaddress from ta3", null);

    eC.moveToFirst();

    toEmails = eC.getString(eC.getColumnIndex("emailaddress"));

    //for contact number from database

    numb = db.rawQuery("SELECT number from ta3", null);

    numb.moveToFirst();

    number1 = numb.getString(numb.getColumnIndex("number"));


    /// for number2

    numb2 = db.rawQuery("SELECT number2 from ta3", null);

    numb2.moveToFirst();

    number2 = numb2.getString(numb2.getColumnIndex("number2"));

    //t1.setText("DB " + serial.toString());
    //t2.setText(getSimSerialNumber.toString());

    if(IsValid())
    {

        Toast.makeText(getApplicationContext(),
                "You information successfully.", Toast.LENGTH_SHORT)
                .show();


        String phoneNo = number1.toString();
        String phoneNo2 = number2.toString();
        String msg = " Company Name: " + getSimOperator + "\n" + "    SIM      Serial Number: " + getSimSerialNumber + "  IME Number: " + IMEINumber;
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, msg, null, null);
            smsManager.sendTextMessage(phoneNo2, null, msg, null, null);
            Toast.makeText(getApplicationContext(), "Message Sent",
                    Toast.LENGTH_LONG).show();
        } catch (Exception ex) {
            Toast.makeText(getApplicationContext(),
                    ex.getMessage().toString(),
                    Toast.LENGTH_LONG).show();
            ex.printStackTrace();
        }


    }

    //onStart(new Intent(Restart.this, CallHistory.class),0);
}

@Override
public void onProviderDisabled(String provider) {
    Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");
}

@Override
public void onLocationChanged(Location location) {

    //txtLat = (TextView) findViewById(R.id.txtLat);
   // txtLat.setText("Latitude:" + location.getLatitude() + "\nLongitude:" + location.getLongitude()+"\nSerial Number: "+"\n"+serialnumber);

    Toast.makeText(
            getApplicationContext(),
            "Latitude:" + location.getLatitude() + "\nLongitude:" + location.getLongitude(),
            Toast.LENGTH_LONG).show();

    toEmailList = Arrays.asList(toEmails
            .split("\\s*,\\s*"));
    Log.i("SendMailActivity", "To List: " + toEmailList);
    emailBody ="  IME Number: " + IMEINumber + "\n" + "   SIM Number: " + getSimSerialNumber+ " Company Name: " + getSimOperator +""+  "Latitude:" + location.getLatitude() + "\nLongitude:" + location.getLongitude();
    emailSubject = "Mobile restrt";

    new SendMailTask(Restart.this).execute(fromEmail, fromPassword, toEmailList, emailSubject, emailBody);

    SystemClock.sleep(10000);

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
}

@Override
public void onStart(Intent intent, int startid) {

 //        Intent nextScreen = new Intent(Restart.this, TaskPerform.class);
  //        startActivity(nextScreen);
   /*        class formail extends Activity{

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

            Intent nextScreen = new Intent(Restart.this, CallHistory.class);
            startActivity(nextScreen);
        }
    }*/
   // startActivity(intent);
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {

}

private boolean IsValid()
{
    if(serial.toString().equals(getSimSerialNumber.toString()) )
        return false;
    else
        return true;
}

/*public class CallHistory extends Activity {

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

        getCallDetails();


    }
    private void getCallDetails() {

        String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
   *//* Query the CallLog Content Provider *//*
        Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
                null, null, strOrder);
        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        sb.append("Call Log :");


        while (managedCursor.moveToNext()) {
            if (counter == 6)
                break;
            ;
            String phNum = managedCursor.getString(number);
            String callTypeCode = managedCursor.getString(type);
            String strcallDate = managedCursor.getString(date);
            Date callDate = new Date(Long.valueOf(strcallDate));
            String callDuration = managedCursor.getString(duration);
            String callType = null;
            int callcode = Integer.parseInt(callTypeCode);
            switch (callcode) {
                case CallLog.Calls.OUTGOING_TYPE:
                    callType = "Outgoing";
                    break;
                case CallLog.Calls.INCOMING_TYPE:
                    callType = "Incoming";
                    break;
                case CallLog.Calls.MISSED_TYPE:
                    callType = "Missed";
                    break;
            }
            sb.append("\nPhone Number:--- " + phNum + " \nCall Type:--- "
                    + callType + " \nCall Date:--- " + callDate
                    + " \nCall duration in sec :--- " + callDuration);
            sb.append("\n----------------------------------");
            counter++;
        }
        managedCursor.close();

    }
   }*/


}

2 个答案:

答案 0 :(得分:0)

为了将您的服务与您的活动联系起来,您必须将其绑定到您的活动,并且您必须考虑的两件大事是

  1. 应该从您的活动开始。
  2. 一旦你的活动被破坏,它就会死亡。
  3. HTTP:refer this link

答案 1 :(得分:-1)

managedQueryService中不存在,无论如何都已弃用。试试getContentResolver().query()