Android中的服务未在BroadCast接收器中启动

时间:2013-03-06 16:15:40

标签: android sqlite broadcastreceiver

我正在尝试创建一个必须执行一些数据库活动的Android应用程序,例如在SQLite DB中存储电话号码和名称等。所有数据库内容都是作为服务完成的。一旦呼叫到来,服务必须立即开始,并且当呼叫结束时,一旦呼叫结束,必须立即向用户显示存储的细节。为此我使用Broad Cast Receiver。我还提供了以下在我的应用中使用的代码。

MyServices.java

public class MyServices extends Service {
TelephonyManager Tel;
MyPhoneStateListener MyListener;
RB_SIGNAL_STRENGTH signalobj = new RB_SIGNAL_STRENGTH();
RB_DatabaseHandler db = new RB_DatabaseHandler(getApplicationContext());

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


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    isMyServiceRunning();
    MyListener = new MyPhoneStateListener();
    Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    super.onCreate();
    Log.i("Serv","Service Started");

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    db.close_DB();
    Log.i("Serv","Service Stopped");
    super.onDestroy();
}

MyReceiver.java

    public class MyPhoneReceiver extends BroadcastReceiver {
String state=null;

@Override
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();

    if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) 
    {
        try{


        Intent i = new Intent(context,MyServices.class);

        Log.i("Recv", "In Try");
        context.startService(i);
        }
        catch(Exception e)
        {
            Log.d("Recv", "Service not starting");
        }
    }//End if offhook
    if(state.equals(TelephonyManager.EXTRA_STATE_IDLE))
    {
        Log.i("Recv","CALL ENDED");
        try
        {
            Intent i = new Intent(context,EndActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            
            context.startActivity(i);
        }
        catch(Exception e){
            Log.d("Recv", "Activity not starting");
        }
    }
    }

}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="9" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.SIGNAL_PERSISTENT_PROCESSES" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity android:name="StartActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="EndActivity" >
    </activity>
            <receiver android:name=".MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" >
            </action>
        </intent-filter>
    </receiver>
    <service 
        android:name="MyServices" 
        ></service>
</application>

2 个答案:

答案 0 :(得分:1)

您的班级名称为MyPhoneReceiver,但在您的清单中,您使用的是MyReceiver,这两个名称必须完全匹配。


<强>加成
我刚刚注意到您在服务具有有效的Context之前尝试实例化您的数据库。这可能会在MyServices中引发异常:

RB_DatabaseHandler db = new RB_DatabaseHandler(getApplicationContext());

您可以将db声明为字段变量,但保留为null:

RB_DatabaseHandler db;

在像onStartCommand()这样的方法中初始化它:

db = new RB_DatabaseHandler(getApplicationContext());

最后,无序调用onCreate()之类的基本方法通常会产生问题,onStartCommand()不推荐这样做:

<击>
super.onCreate();

<击>

答案 1 :(得分:1)

因为state变量为null

public class MyPhoneReceiver extends BroadcastReceiver {
String state=null;

@Override
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    //your code here...