每次手机有来电时,我都想开始一项活动,并在活动中显示有关号码的一些详细信息或接受/拒绝来电。 问题是,每次我接到电话时,活动都不会开始。
这是我的代码:
public class PhoneCallReceiver extends AriesMobileBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
super.onReceive(context, intent);
if(!preferences.getBoolean(Preferences.CALL_SHOW_CUSTOMER_INFOS, false))
return;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener() {
public void onCallStateChanged(int state, final String incomingNumber){
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING: " + incomingNumber);
final Intent intent = new Intent(context, PhoneCallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(PhoneCallActivity.EXTRA_PHONE_NUMBER, incomingNumber);
new Thread() {
public void run() {
long now = AriesUtils.currentTimeStamp();
try {
Thread.sleep(500);
} catch(InterruptedException e) {}
now = AriesUtils.currentTimeStamp() - now;
Log.e(TAG, "ELAPSED TIME: " + now);
context.startActivity(intent);
}
}.start();
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
活动(实际上,什么都不做):
public class PhoneCallActivity extends AriesMobileActivity {
public static final String EXTRA_PHONE_NUMBER = "phone_number";
private TextView tvPhoneCallContact;
private TextView tvPhoneCallNumber;
private TextView tvPhoneCallSystem;
private ImageView imgPhoneCallStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "PHONE CALL ACTIVITY STARTED");
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
window.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
setContentView(R.layout.layout_phone_call);
Intent intent = getIntent();
if(!intent.hasExtra(EXTRA_PHONE_NUMBER)) {
finish();
return;
}
String phoneNumber = intent.getStringExtra(EXTRA_PHONE_NUMBER);
phoneNumber = phoneNumber.replace("+39", "").replace(" ", "");
CustomerContact[] contacts = new CustomerContact.Provider(context).findByPhoneNumber(phoneNumber);
if(contacts.length <= 0) {
finish();
return;
}
}
和布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refuse" />
</LinearLayout>
所以我的要求是: 1-如何在手机响铃时启动活动? 2-如何管理回复/拒绝按钮? 3-如果这个解决方案不可能/不好/愚蠢是否有任何其他方式在收到呼叫时显示信息? (例如某种通知)
提前致谢
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ngs.ariesmobile"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".AriesMobileApplication"
android:allowBackup="true"
android:icon="@drawable/app_logo"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light.NoActionBar" >
<receiver android:name=".receivers.PhoneCallReceiver" >
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity
android:name=".view.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".view.HomeActivity"
android:label="@string/app_name" />
<activity
android:name=".view.SignatureCaptureActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name"
android:screenOrientation="sensorLandscape" />
<activity
android:name=".view.SetupHomeActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.SetupApiActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.SetupLoginActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.SetupTechnicianActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.SettingsActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.SettingsPasswordActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.SettingsApiActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.SynchronizationActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.ReportCustomerActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name"
android:screenOrientation="sensorLandscape" />
<activity
android:name=".view.ReportInternalActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name"
android:screenOrientation="sensorLandscape" />
<activity
android:name=".view.ReportListActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.TicketListActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.PhoneCallActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent" />
<activity
android:name=".view.SystemActivity"
android:configChanges="orientation|keyboard|screenSize"
android:screenOrientation="landscape"
android:label="@string/app_name" />
<activity
android:name=".view.SystemListActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/app_name" />
<activity
android:name=".view.ReportSummaryActivity"
android:configChanges="orientation|keyboard|screenSize"
android:screenOrientation="portrait"
android:label="@string/app_name" />
<activity
android:name=".view.ManualsListActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/title_activity_manuals_list" >
</activity>
<activity
android:name=".view.SystemStoricalActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/title_activity_system_storical" >
</activity>
<activity
android:name=".view.SystemTechnicalPartActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/title_activity_system_technical_part" >
</activity>
<activity
android:name=".view.SystemConsActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/title_activity_system_cons" >
</activity>
<activity
android:name=".view.SystemNoteActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="@string/title_activity_system_note" >
</activity>
<activity
android:name=".view.SystemSupervisionActivity"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_system_supervision" >
</activity>
<activity
android:name=".view.CalendarActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="landscape"
android:label="@string/title_activity_calendar" >
</activity>
<activity
android:name=".view.ReportHoursActivity"
android:label="@string/title_activity_report_hours" >
</activity>
<!--- <activity
android:name="ngs.ariesmobile.view.ReportHoursActivity"
android:label="@string/title_activity_report_hours" >
</activity> -->
</application>
</manifest>