0

我已经用 4.3 编译了我的应用程序,设置了 min sdk 2.2 并设置了目标 sdk 4.3。当我在模拟器上运行它时,它可以正常工作,但是当我在真实设备上运行它时,它会在服务调用时崩溃。没看懂,问题出在哪里?我试图调试它,但我仍然没有任何解决方案。有人可以帮我解决这个问题吗?我的应用程序代码如下,Schedule.javaScheduleService.java

package com.si.autoprofiler.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.si.autoprofiler.R;

import com.si.autoprofiler.DataBase.DBAdapterProfile2;
import com.si.autoprofiler.DataBase.DBAdapterSchedule;
import com.si.autoprofiler.util.ProfileGetSet;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

public class Schedule extends Activity {
Calendar calender = Calendar.getInstance();
Button getDate, getStartTime, getEndTime, setBtn, back;
EditText getDescription;
Spinner profileSpin;

DBAdapterSchedule db;
DBAdapterProfile2 dbpro;

List<ProfileGetSet> scheduleList;
ArrayList<String> scheduleProfilelist;
ArrayAdapter<String> scheduleProfileAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    // this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.schedule);

    db = new DBAdapterSchedule(Schedule.this);

    dbpro = new DBAdapterProfile2(Schedule.this);

    getDate = (Button) findViewById(R.id.setdate);
    getStartTime = (Button) findViewById(R.id.starttime);
    getEndTime = (Button) findViewById(R.id.endtime);
    setBtn = (Button) findViewById(R.id.save);
    back = (Button) findViewById(R.id.backBtn);

    scheduleProfilelist = new ArrayList<String>();
    createDynamicSpinner1();

    getDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            new DatePickerDialog(Schedule.this, d, calender
                    .get(Calendar.YEAR),          calender.get(Calendar.MONTH),
                    calender.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

    getStartTime.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            new TimePickerDialog(Schedule.this, t1, calender
                    .get(Calendar.HOUR_OF_DAY), calender
                    .get(Calendar.MINUTE), false).show();
        }
    });

    getEndTime.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            new TimePickerDialog(Schedule.this, t2, calender
                    .get(Calendar.HOUR_OF_DAY), calender
                    .get(Calendar.MINUTE), false).show();
        }
    });

    setBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String setDate = getDate.getText().toString();
            String startTime = getStartTime.getText().toString();
            String endTime = getEndTime.getText().toString();
            String selectedprofile = profileSpin.getSelectedItem()
                    .toString();

            db.open();
            Cursor c1 = db.getallSchedule();
            if (c1 != null) {
                c1.moveToFirst();
                db.deleteAllSchedule();
                db.insertSchedule(setDate, startTime, endTime,
                        selectedprofile);
                Toast.makeText(getApplicationContext(), "Schedule is set.",
                        Toast.LENGTH_SHORT).show();
            } else if (c1 == null) {
                db.insertSchedule(setDate, startTime, endTime,
                        selectedprofile);
                Toast.makeText(getApplicationContext(), "Schedule is set.",
                        Toast.LENGTH_SHORT).show();
            }
            c1.close();
            db.close();
            Intent in = new Intent(Schedule.this, ScheduleService.class);
            startService(in);
        }
    });

}

private void createDynamicSpinner1() {

    profileSpin = (Spinner) findViewById(R.id.profileSpinner1);
    dbpro.open();
    scheduleList = dbpro.getAllList();

    for (ProfileGetSet p : scheduleList) {
        String log1 = p.getProfilename();

        scheduleProfilelist.add(log1);

    }

    scheduleProfileAdapter = new ArrayAdapter<String>(
            getApplicationContext(),
            R.layout.spinner_text,
            scheduleProfilelist);
    profileSpin.setAdapter(scheduleProfileAdapter);
    dbpro.close();
}

/* ...................Date time picker dialog code here............ */

TimePickerDialog.OnTimeSetListener t1 = new TimePickerDialog.OnTimeSetListener() {

    @SuppressLint("SimpleDateFormat")
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        calender.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calender.set(Calendar.MINUTE, minute);

        SimpleDateFormat timeformate = new SimpleDateFormat("hh:mm:ss aa");
        String s1 = timeformate.format(calender.getTime());

        getStartTime.setText(s1);

    }
};

TimePickerDialog.OnTimeSetListener t2 = new TimePickerDialog.OnTimeSetListener() {

    @SuppressLint("SimpleDateFormat")
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        calender.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calender.set(Calendar.MINUTE, minute);

        SimpleDateFormat timeformate = new SimpleDateFormat("hh:mm:ss aa");
        String s2 = timeformate.format(calender.getTime());
        getEndTime.setText(s2);
    }
};

DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        calender.set(Calendar.YEAR, year);
        calender.set(Calendar.MONTH, monthOfYear);
        calender.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        java.text.DateFormat datef = java.text.DateFormat.getDateInstance();
        String d1 = datef.format(calender.getTime());
        getDate.setText(d1);

    }
};

public void backbtn(View Button) {

    finish();
}
}

服务类代码如下:

package com.si.autoprofiler.view;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

import com.si.autoprofiler.R;

import com.si.autoprofiler.DataBase.DBAdapterProfile2;
import com.si.autoprofiler.DataBase.DBAdapterSchedule;

import android.annotation.SuppressLint;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Vibrator;
import android.provider.MediaStore.Audio;
import android.text.format.Time;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class ScheduleService extends Service {
private static Timer timer1 = new Timer();
DBAdapterSchedule db = new DBAdapterSchedule(ScheduleService.this);
Vibrator vib = null;
Boolean bool = false;

DBAdapterProfile2 DBProfile = new DBAdapterProfile2(ScheduleService.this);
AudioManager am = null;

Time timeNow = new Time();

Date d2, d3, d1;

String da, start, end, profile, vibrate;
int ringValue, alarmValue, notiValue, sysValue, voiceValue;
String convertedTime, endTime, convertedEndTime;
String dissmis = "Dissmis";

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}

@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);

    db.open();
    Cursor c1 = db.getallSchedule();
    if (c1 != null) {
        c1.moveToFirst();
        da = c1.getString(0);
        start = c1.getString(1);
        end = c1.getString(2);
        profile = c1.getString(3);
    }
    c1.close();
    db.close();

    timer1.scheduleAtFixedRate(new MainTask(), 0, 1000);

}

private class MainTask extends TimerTask {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        toastHandler.sendEmptyMessage(0);
    }

}

private final Handler toastHandler = new Handler() {
    @SuppressWarnings("deprecation")
    @SuppressLint({ "HandlerLeak", "HandlerLeak", "HandlerLeak",
            "HandlerLeak", "HandlerLeak", "SimpleDateFormat" })
    public void handleMessage(android.os.Message msg) {
        Calendar cal = Calendar.getInstance(TimeZone
                .getTimeZone("GMT+1:00"));
        Date currentLocalTime = cal.getTime();
        DateFormat date = new SimpleDateFormat("hh:mm:ss a");
        // DateFormat date2 = new SimpleDateFormat("hh:mm:ss a");
        // endTime = date2.format(currentLocalTime);

        convertedTime = date.format(currentLocalTime);

        DateFormat s = new SimpleDateFormat("MMMM d, yyyy");
        String d = s.format(currentLocalTime);

        try {
            d2 = s.parse(da);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            d3 = s.parse(d);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int dateValue = d2.compareTo(d3);

        if (dateValue < 1) {

            if (start.equals(convertedTime)) {
                Toast.makeText(getApplicationContext(), "Inside if",
                        Toast.LENGTH_SHORT).show();
                DBProfile.open();
                Cursor c2 = DBProfile.getProfile(profile);
                if (c2 != null) {

                    ringValue = Integer.parseInt(c2.getString(1));
                    alarmValue = Integer.parseInt(c2.getString(2));
                    notiValue = Integer.parseInt(c2.getString(3));
                    sysValue = Integer.parseInt(c2.getString(4));
                    voiceValue = Integer.parseInt(c2.getString(5));
                    vibrate = c2.getString(6);
                }
                c2.close();
                DBProfile.close();

                bool = Boolean.valueOf(vibrate);

                if (bool) {

                    if (ringValue == 0 && alarmValue == 0 && notiValue == 0
                            && sysValue == 0 && voiceValue == 0) {

                        am.setStreamVolume(AudioManager.STREAM_RING,
                                ringValue, 0);
                        am.setStreamVolume(AudioManager.STREAM_ALARM,
                                alarmValue, 0);
                        am.setStreamVolume(
                                AudioManager.STREAM_NOTIFICATION,
                                notiValue, 0);
                        am.setStreamVolume(AudioManager.STREAM_SYSTEM,
                                sysValue, 0);
                        am.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                                voiceValue, 0);
                        am.setVibrateSetting(
                                AudioManager.VIBRATE_TYPE_RINGER,
                                AudioManager.VIBRATE_SETTING_ON);
                        am.setVibrateSetting(
                                AudioManager.VIBRATE_TYPE_NOTIFICATION,
                                AudioManager.VIBRATE_SETTING_ON);

                    } else {

                        am.setStreamVolume(AudioManager.STREAM_RING,
                                ringValue, 0);
                        am.setStreamVolume(AudioManager.STREAM_ALARM,
                                alarmValue, 0);
                        am.setStreamVolume(
                                AudioManager.STREAM_NOTIFICATION,
                                notiValue, 0);
                        am.setStreamVolume(AudioManager.STREAM_SYSTEM,
                                sysValue, 0);
                        am.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                                voiceValue, 0);
                        am.setVibrateSetting(
                                AudioManager.VIBRATE_TYPE_RINGER,
                                AudioManager.VIBRATE_SETTING_ON);
                        am.setVibrateSetting(
                                AudioManager.VIBRATE_TYPE_NOTIFICATION,
                                AudioManager.VIBRATE_SETTING_ON);
                    }
                } else {

                    am.setStreamVolume(AudioManager.STREAM_RING, ringValue,
                            0);
                    am.setStreamVolume(AudioManager.STREAM_ALARM,
                            alarmValue, 0);
                    am.setStreamVolume(AudioManager.STREAM_NOTIFICATION,
                            notiValue, 0);
                    am.setStreamVolume(AudioManager.STREAM_SYSTEM,
                            sysValue, 0);
                    am.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                            voiceValue, 0);
                    am.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
                            AudioManager.VIBRATE_SETTING_OFF);
                    am.setVibrateSetting(
                            AudioManager.VIBRATE_TYPE_NOTIFICATION,
                            AudioManager.VIBRATE_SETTING_OFF);

                }

            }
        }

        if (end.equals(convertedTime)) {

            timer1.cancel();

            onDestroy();

        }

    };

};

@SuppressWarnings("deprecation")
@Override
public void onDestroy() {
    am.setStreamVolume(AudioManager.STREAM_RING, 7, 0);
    am.setStreamVolume(AudioManager.STREAM_ALARM, 7, 0);
    am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 7, 0);
    am.setStreamVolume(AudioManager.STREAM_SYSTEM, 7, 0);
    am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 5, 0);
    am.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
            AudioManager.VIBRATE_SETTING_ON);
    am.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
            AudioManager.VIBRATE_SETTING_ON);
    super.onDestroy();

}
}
4

0 回答 0