0

我正在尝试制作一个必须执行一些数据库活动的 android 应用程序,例如在 SQLite DB 中存储电话号码和姓名等。所有数据库的东西都是作为服务完成的。服务必须在呼叫到来时立即启动,并且当呼叫结束时,存储的详细信息必须在呼叫结束后立即显示给用户。为此,我正在使用广播接收器。我还提供了我在我的应用程序中使用的以下代码。

我的服务.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>

4

2 回答 2

1

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


另外
我刚刚注意到您正在尝试在服务具有有效上下文之前实例化您的数据库。这可能会在 MyServices 中引发异常:

RB_DatabaseHandler db = new RB_DatabaseHandler(getApplicationContext());

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

RB_DatabaseHandler db;

onStartCommand()初始化它的方法中:

db = new RB_DatabaseHandler(getApplicationContext());

最后,调用诸如onCreate()乱序之类的基本方法通常会产生问题,不建议这样做onStartCommand()

super.onCreate();

于 2013-03-06T16:22:38.283 回答
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...
于 2013-03-06T16:24:31.357 回答