0
我見過不同的人在他們的類中實現了onReceive()
,它擴展了BroadcastReceiver
。但是有沒有一種方法可以在與MainActivity
不同的類中實現onReceive()
,當在MainActivity
中按下按鈕時?如果可能的話,當按下按鈕時我將如何撥打onReceive()
?我在我的AndroidManifest
文件中也有一個接收器,當按下按鈕並調用onReceive()
時,也會觸發該接收器嗎?當MainActivity中的按鈕被按下時實現onReceive()
MainActivity類別 -
public class MainActivity extends Activity {
Button activateButton;
LocationManager mManager;
AlertDialog.Builder box;
BroadcastReceiver b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activateButton = (Button)findViewById(R.id.activate);
activateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogBox();
}
});
}
protected void DialogBox() {
box = new AlertDialog.Builder(this);
box.setTitle("Reject incoming calls?").
setMessage("On activation, your phone will reject all incoming calls").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent("android.intent.action.PHONE_STATE");
MainActivity.this.sendBroadcast(intent);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog alert = box.create();
alert.show();
}
RejectCall類 -
public class RejectCall extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.i("RejectClass", "Triggered");
ITelephony telephonyService;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
//telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
的AndroidManifest.xml -
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.scimet.admin.driveon" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".RejectCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我也爲ITelephony定義了一個接口。
嗨,我的手機剛崩潰上實施的方式。手機重新啓動。我將編輯描述併發布我的代碼。 – Slay 2014-10-02 21:06:19
查看我的編輯,您需要保存偏好以打開或關閉拒絕。系統在收到呼叫時發送廣播。 – JanKnotek 2014-10-02 21:35:40
是不是它應該是'preferences.edit()。putBoolean(「PREF_REJECT_CALLS」,true).commit();'對不起,我可能完全錯了。另外,這是如何保存偏好? – Slay 2014-10-02 21:43:45