我正在尝试覆盖来电屏幕 - 我知道我无法更改它,所以我试图在顶部弹出一个活动。
我的代码工作正常,除非手机空闲了几分钟。
我的代码:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="7"
android:versionName="7">
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="10"></uses-sdk>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<receiver android:name=".MyPhoneBroadcastReceiver" android:enabled="true">
<intent-filter android:priority="99999">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity
android:name=".Call" >
</activity>
</application>
</manifest>
MyPhoneBroadcastReceiver.java:
public class MyPhoneBroadcastReceiver extends BroadcastReceiver{
public void onReceive(final Context context, Intent intent) {
Thread pageTimer = new Thread(){
public void run(){
try{
sleep(700);
} catch (InterruptedException e){
e.printStackTrace();
} finally {
Intent i = new Intent();
i.setClass(context, Call.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
i.putExtra("INCOMING_NUMBER", incomingNumber);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
}
}
};
pageTimer.start();
}
}
调用.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
public class Call extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow(). addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
getWindow().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
setContentView(R.layout.main);
}
}
顺便说一句-我尝试在睡眠(700)之前等待屏幕唤醒,但没有帮助(在 MyPhoneBroadcastReceiver.java 中)
...
try {
if (pm.isScreenOn()) {
sleep(700);
} else {
while (!pm.isScreenOn()) {
// Do nothing...
}
sleep(700);
}
}
...