Android Dummy需要帮助:)
我正在尝试拨打一个popUp窗口,无论何时收到呼叫或通过电话拨打电话,都会播放视频。
通过一点冲浪和在互联网上查找,我可以在收到或拨打电话时创建Toast。
参考:http://www.codeproject.com/Articles/548416/Detecting-incoming-and-outgoing-phone-calls-on-And
其次能够创建一个PopUp,但现在无论何时收到或发出呼叫,我都无法调用PopUp。
我的MainActivity.java
package com.shivamkotwalia.video;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private boolean detectEnabled;
private TextView textViewDetectState;
private Button buttonToggleDetect;
private Button buttonExit;
// private VideoView myVideoView;
// private int position = 0;
// private ProgressDialog progressDialog;
// MediaController mediaControls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start PopUp
final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
popupView.setOnTouchListener(new OnTouchListener() {
int orgX, orgY;
int offsetX, offsetY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
orgX = (int) event.getX();
orgY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
offsetX = (int)event.getRawX() - orgX;
offsetY = (int)event.getRawY() - orgY;
popupWindow.update(offsetX, offsetY, -1, -1, true);
break;
}
return true;
}});
}
});
// Stop PopUp
// Calling Detection Process //
textViewDetectState = (TextView) findViewById(R.id.textViewDetectState);
buttonToggleDetect = (Button) findViewById(R.id.buttonDetectToggle);
buttonToggleDetect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setDetectEnabled(!detectEnabled);
}
});
buttonExit = (Button) findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
System.exit(0);
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void setDetectEnabled(boolean enable) {
detectEnabled = enable;
Intent intent = new Intent(this, CallDetectService.class);
if (enable) {
// start detect service
startService(intent);
buttonToggleDetect.setText("Turn off");
textViewDetectState.setText("Service Active");
} else {
// stop detect service
stopService(intent);
buttonToggleDetect.setText("Turn on");
textViewDetectState.setText("Service Inactive");
}
}
}
我的CallHelper.java
package com.shivamkotwalia.video;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class CallHelper {
/* Code that we need to run when the Call is received instead of creating a TOAST
public class popUpNew extends Activity{
protected void onCreate() {
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
//@Override
public void onClick(View v) {
popupWindow.dismiss();
}});
//popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
popupView.setOnTouchListener(new OnTouchListener() {
int orgX, orgY;
int offsetX, offsetY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
orgX = (int) event.getX();
orgY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
offsetX = (int)event.getRawX() - orgX;
offsetY = (int)event.getRawY() - orgY;
popupWindow.update(offsetX, offsetY, -1, -1, true);
break;
}
return true;
}});
}
}
*/
/**
* Listener to detect incoming calls.
*/
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
Toast.makeText(ctx,
"App By Shivam Incoming: " + incomingNumber,
Toast.LENGTH_LONG).show();
break;
}
}
}
/**
* Broadcast receiver to detect the outgoing calls.
*/
public class OutgoingReceiver extends BroadcastReceiver {
public OutgoingReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
// /called when someone is making a call
Toast.makeText(ctx, "App By Shivam Outgoing: " + number,
Toast.LENGTH_LONG).show();
}
}
private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
private OutgoingReceiver outgoingReceiver;
public CallHelper(Context ctx) {
this.ctx = ctx;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}
/**
* Start calls detection.
*/
public void start() {
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter intentFilter = new IntentFilter(
Intent.ACTION_NEW_OUTGOING_CALL);
ctx.registerReceiver(outgoingReceiver, intentFilter);
}
/**
* Stop calls detection.
*/
public void stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
ctx.unregisterReceiver(outgoingReceiver);
}
}
在拨打或拨打电话时,需要在第21至67行的上述代码中调用吗?
PhoneStateListener
提前致谢!!!