我正在嘗試在應用程序上創建。我需要檢測我是否接到任何電話,然後獲取號碼,但是這個過程應該在後臺進行。
後臺工作是後期工作,但現在我正在嘗試啓動我的應用程序,但它崩潰了。
你能告訴我如何在背景中調用broadcastreciver嗎? 這裏是我的MainActivity.java如何檢測後臺來電
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstancestate)
{
super.onCreate(savedInstancestate);
setContentView(R.layout.activity_main);
}
public void broadcastIntent(View view)
{
Intent intent = new Intent();
intent.setAction("com.app.callrecord.MyBroadcastReceiver");
sendBroadcast(intent);
}
}
Broadcastreceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null)
return;
String phoneNumber = null;
// Incoming call
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if ((state != null) && (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
// callToast(phoneNumber);
}
// Outgoing call
else if (state == null) {
Intent i = new Intent(context,RecordHistory.class);
intent.putExtra("phonenumber", phoneNumber);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
// Here: do something with the number
}
}
}
這裏是我的清單文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.app.callrecord.MainActivity">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RecordHistory"></activity>
</application>
而且這裏是我的例外日誌
02-18 13:36:17.240: E/AndroidRuntime(9397): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.callrecord/com.app.callrecord.MyBroadCastReciever}: java.lang.ClassNotFoundException: com.app.callrecord.MyBroadCastReciever
02-18 13:36:17.240: E/AndroidRuntime(9397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-18 13:36:17.240: E/AndroidRuntime(9397): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2278)
02-18 13:36:17.240: E/AndroidRuntime(9397): at android.app.ActivityThread.access$600(ActivityThread.java:142)
02-18 13:36:17.240: E/AndroidRuntime(9397): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
給我任何參考。
謝謝您的回答,但是當我啓動的應用程序可以在我的手機打電話我沒拿到,我用它來顯示號碼的任何祝酒。 .. – 2013-02-18 08:22:45
這可能是由於缺少權限。我編輯了答案。 – Tomik 2013-02-18 08:49:08