Android 应用程序无法启动服务

3

嘿,我正在尝试在我的Android应用程序上实现一个服务。该服务必须执行与Activity相同的任务。也就是说,如果CallLog.Calls内容提供程序发生更改,则必须通知服务并将数据插入数据库,即使应用程序未运行。我的意思是,服务将在应用程序启动后运行,因此,如果应用程序被杀死,服务将继续运行,直到操作系统停止它,对吗?

因此,它将在后台运行,收集CallLog.Calls服务中更改的所有数据。但是,服务没有运行。我在Activity的onCreate()方法中启动它。并且在Service内部,我实现了一个ContentObserver类,该类使用onChange()方法以防止CallLog.Calls内容提供程序中的任何更改。

我不知道的是为什么服务没有启动,以及为什么即使我在DDMS透视图上杀死了应用程序,它也无法正常工作。

以下是代码。

Activity名为 RatedCalls.java

public class RatedCalls extends ListActivity {

private static final String LOG_TAG = "RATEDCALLSOBSERVER";
private Handler handler = new Handler();
private SQLiteDatabase db;
private CallDataHelper cdh;
StringBuilder sb = new StringBuilder();
OpenHelper openHelper = new OpenHelper(RatedCalls.this);
private Integer contentProviderLastSize;
private Integer contentProviderCurrentSize;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    cdh = new CallDataHelper(this);
    db = openHelper.getWritableDatabase();

    startService(new Intent(this, RatedCallsService.class));
    registerContentObservers();

    Log.i("FILLLIST", "calling from onCreate()");

    Cursor cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");

    contentProviderLastSize = cursor.getCount();

}

class RatedCallsContentObserver extends ContentObserver {
    public RatedCallsContentObserver(Handler h) {
        super(h);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;

    }

    @Override
    public void onChange(boolean selfChange) {
        Log.d(LOG_TAG, "RatedCallsContentObserver.onChange( " + selfChange
                + ")");
        super.onChange(selfChange);
        searchInsert();

    }
}

private void searchInsert() {

    Cursor cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");

    Log.i("FILLLIST", "Calling from searchInsert");

    startManagingCursor(cursor);
    int numberColumnId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    int durationId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.DURATION);
    int contactNameId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    int numTypeId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

    Date dt = new Date();
    int hours = dt.getHours();
    int minutes = dt.getMinutes();
    int seconds = dt.getSeconds();
    String currTime = hours + ":" + minutes + ":" + seconds;

    SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy");
    Date date = new Date();

    cursor.moveToFirst();           

            String contactNumber = cursor.getString(numberColumnId);
            String contactName = cursor.getString(contactNameId);
            String duration = cursor.getString(durationId);
            String numType = cursor.getString(numTypeId);
            stopManagingCursor(cursor);
            ContentValues values = new ContentValues();

            values.put("contact_id", 1);
            values.put("contact_name", contactName);
            values.put("number_type", numType);
            values.put("contact_number", contactNumber);
            values.put("duration", duration);
            values.put("date", dateFormat.format(date));
            values.put("current_time", currTime);
            values.put("cont", 1);

            db.insert(CallDataHelper.TABLE_NAME, null, values);         
}
    public void registerContentObservers() {

    this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new RatedCallsContentObserver(handler));

}

这是一个名为RatedCallsService.java的服务。

public class RatedCallsService extends Service {

private static final String TAG = "RatedCallsService";
private static final String LOG_TAG = "RatedCallsService";
private Handler handler = new Handler();
private SQLiteDatabase db;
private CallDataHelper cdh;
OpenHelper openHelper = new OpenHelper(RatedCallsService.this);

class RatedCallsContentObserver extends ContentObserver {
    public RatedCallsContentObserver(Handler h) {
        super(h);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;

    }

    @Override
    public void onChange(boolean selfChange) {
        Log.d(LOG_TAG, "RatedCallsContentObserver.onChange( " + selfChange
                + ")");
        super.onChange(selfChange);
        searchInsert();

    }
}

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

@Override
public void onCreate() {

    Toast.makeText(this, "Rated Calls Service Created", Toast.LENGTH_LONG).show();
    Log.i(TAG, "onCreate");
    registerContentObservers();

}

@Override
public void onDestroy() {

    Toast.makeText(this, "Rated Calls Service Stopped", Toast.LENGTH_LONG).show();
    Log.i(TAG, "onDestroy");
    cdh = new CallDataHelper(this);
    db = openHelper.getWritableDatabase();

}

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

    Toast.makeText(this, "Rated Calls Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");      
    registerContentObservers();

}

private void searchInsert() {

    Cursor cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");

    Log.i("FILLLIST", "Calling from searchInsert");

    int numberColumnId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    int durationId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.DURATION);
    int contactNameId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    int numTypeId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

    Date dt = new Date();
    int hours = dt.getHours();
    int minutes = dt.getMinutes();
    int seconds = dt.getSeconds();
    String currTime = hours + ":" + minutes + ":" + seconds;

    SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy");
    Date date = new Date();

    if (cursor.moveToFirst()) {
        do {

            String contactNumber = cursor.getString(numberColumnId);
            String contactName = cursor.getString(contactNameId);
            String duration = cursor.getString(durationId);
            String numType = cursor.getString(numTypeId);           
            ContentValues values = new ContentValues();

            values.put("contact_id", 1);
            values.put("contact_name", contactName);
            values.put("number_type", numType);
            values.put("contact_number", contactNumber);
            values.put("duration", duration);
            values.put("date", dateFormat.format(date));
            values.put("current_time", currTime);
            values.put("cont", 1);

            db.insert(CallDataHelper.TABLE_NAME, null, values);

        } while (cursor.moveToNext());
        cursor.close();
    }
}


public void registerContentObservers() {

    this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new RatedCallsContentObserver(handler));

}

 }
4个回答

19

请查看您的清单文件中是否已添加此服务......

谢谢......


5

在您的清单中声明服务时,请尝试在清单中使用服务类的完整包位置。

例如:<service android:name="com.company.project.package.MyService">

我的服务没有启动,这对我有用。


1

嗨,与其使用服务和内容观察器,我建议观察手机状态。观察手机状态可以触发您的更新服务。

您需要

android.permission.READ_PHONE_STATE

权限问题,不是大事。

广播接收器的代码是

    public class CallStateWatcher extends BroadcastReceiver
{



    @Override
    public void onReceive(Context context, Intent intent)
    {

        if (intent.getAction().equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED))
        {
            String extra = intent.getStringExtra(android.telephony.TelephonyManager.EXTRA_STATE);

            if (extra.equals(android.telephony.TelephonyManager.EXTRA_STATE_OFFHOOK))
            {
                // do something
            }
            if (extra.equals(android.telephony.TelephonyManager.EXTRA_STATE_IDLE))
            {
                // do something

            }   

        }
    }
}

你必须定义那个接收器

<receiver
        android:name=".core.watcher.CallStateWatcher">
        <intent-filter>
            <action
                android:name="android.intent.action.PHONE_STATE"></action>

        </intent-filter>
    </receiver>

1
你可能想查看服务生命周期文档。如果你调用Context.startService(),服务应该启动并一直运行,直到有人告诉它停止。
从你的代码示例中看,你正在这样做。你觉得服务没有启动是什么原因?
我不确定当你杀死应用程序时你期望发生什么...这听起来是它无法工作的一个很好的理由。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接