【发布时间】:2014-08-21 06:54:03
【问题描述】:
我想创建一个应用程序,每当我接到来电时,它应该执行以下操作,我的应用程序应该切断来电并向来电号码发送自定义 SMS。我怎样才能做到这一点?在这个应用程序中,我希望有一个 UI 可以在其中切换状态 ON 和 OFF,应该有一个 Edit 文本来提供我的自定义消息。 我的用户界面:
<i>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/r" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Welcome to call Handler"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="All Calls will Automatically Handled" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:text="ToggleButton"
android:textOff="Tap To Turn ON"
android:textOn="Tap To Turn OFF" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="App BY Rishi Serumadar 2014"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="Clear" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/ed1"
android:text="Save" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/toggleButton1"
android:text="Enter The text you would like to sent by defult"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:text="View Callers" />
<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
</i>
我的主要活动
package com.example.first_app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
Button savebtn,viewcallerbtn,clearbtn;
String typedmsg,incomingNumber;
EditText msgbox;
ToggleButton toggleButton1;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
savebtn = (Button)findViewById(R.id.button1);
tv = (TextView)findViewById(R.id.textView3);
clearbtn = (Button)findViewById(R.id.button3);
viewcallerbtn = (Button)findViewById(R.id.button2);
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
toggleButton1.setChecked(false);
clearbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
msgbox.setText("");
}
});
msgbox=(EditText)findViewById(R.id.ed1);
toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(!isChecked)
{
msgbox.setVisibility(100);
savebtn.setVisibility(100);
clearbtn.setVisibility(100);
tv.setVisibility(100);
AudioManager am;
am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(getApplicationContext(), "App Disabled an Ringer set to Normal Mode", Toast.LENGTH_LONG).show();
}
else
{
msgbox.setVisibility(0);
savebtn.setVisibility(0);
clearbtn.setVisibility(0);
tv.setVisibility(0);
AudioManager am;
am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(getApplicationContext(), "App Enabled an Ringer set to Silent Mode", Toast.LENGTH_LONG).show();
}
}
});
savebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
typedmsg = msgbox.getText().toString();
final String togglestatus ;
// togglestatus = toggleButton1.getText().toString();
Toast.makeText(getApplicationContext(),"typed stored is"+incomingNumber +" this ", Toast.LENGTH_SHORT).show();
}
});
viewcallerbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String typedmsg;
typedmsg = msgbox.getText().toString();
Intent intob = new Intent(MainActivity.this,Callers.class);
startActivity(intob);
Toast.makeText(getApplicationContext(), typedmsg, Toast.LENGTH_SHORT).show();
}
});
}
}
我的第二个活动
public class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i("hi", "RINGING 2, number: " + incomingNumber);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(incomingNumber, null, "im busy", null, null);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
//Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
//Log.i(LOG_TAG, "IDLE number");
}
}
}
到目前为止,我的应用程序运行良好。但我收到的短信是我在 onCallStateChanged 方法的第二个活动中给出的。但我希望我在编辑文本中输入的内容应该作为短信内容。
现在我的问题很简单,在我的第一个活动中我有一个字符串 变量“Typedmsg”将获取我在编辑中输入的内容 文本,它将存储在其中。在这里我想要这个 typedmsg 字符串 我在 PhoneCallListener 中的第二个活动中的短信内容。 如何将 typedmsg 内容传递给 onCallStateChanged 方法?
【问题讨论】:
-
我可以知道我收到反对票的原因吗???对我有帮助!!
标签: android broadcastreceiver call smsmanager phone-state-listener