我想创建一个应用程序,每当我收到来电时应该执行以下操作,我的应用程序应该切断来电并向来电号码发送自定义短信。我怎样才能做到这一点?在这个应用程序中,我希望有一个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方法中的第二个活动中给出的。但我希望我在编辑文本中键入的内容应该是短信内容。
现在我的问题非常简单,在我的第一个活动中我有一个字符串 变量&#34; Typedmsg&#34;这将得到我在编辑中输入的内容 文本,它将存储在其中。在这里我想要这个typedmsg字符串 我在PhoneCallListener中的第二个活动中的短信内容。 如何将typedmsg内容传递给onCallStateChanged方法?
答案 0 :(得分:0)
试试此代码
public class MainActivity extends Activity {
private BroadcastReceiver broad;
ToggleButton togglebtn;
Button viewbtn,savebtn,clearbtn;
EditText ed1;
String typedmsg;
SmsManager smsManager = SmsManager.getDefault();
//String appstatus = togglebtn.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
togglebtn=(ToggleButton)findViewById(R.id.toggleButton1);
savebtn=(Button)findViewById(R.id.button1);
viewbtn=(Button)findViewById(R.id.button3);
clearbtn=(Button)findViewById(R.id.button2);
ed1=(EditText)findViewById(R.id.editText1);
typedmsg=ed1.getText().toString();
togglebtn.setChecked(true);
broad = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// This code will execute when the phone has an incoming call
// get the phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
if(togglebtn.isEnabled())
{
smsManager.sendTextMessage(incomingNumber, null, typedmsg, null,null);
}
}
}
};
registerReceiver(broad, new IntentFilter("android.intent.action.PHONE_STATE"));
clearbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ed1.setText("");}});
final AudioManager am ;
am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
//final int mode = am.getRingerMode();
togglebtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
savebtn.setVisibility(1);
viewbtn.setVisibility(1);
clearbtn.setVisibility(1);
ed1.setVisibility(1);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(getApplicationContext(),"App Enabled and Sound profile se to Silent", Toast.LENGTH_SHORT).show();
}
else
{
savebtn.setVisibility(4);
viewbtn.setVisibility(4);
clearbtn.setVisibility(4);
ed1.setVisibility(4);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(getApplicationContext(),"App Disabled and Sound profile se to Normal", Toast.LENGTH_SHORT).show();
}
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
Toast.makeText(getApplicationContext(), "Settings seleted", Toast.LENGTH_SHORT).show();
return true;
}else
if(id==R.id.a1){
Toast.makeText(getApplicationContext(), "Help seleted", Toast.LENGTH_SHORT).show();
return true;
}else
if (id==R.id.a2){
Toast.makeText(getApplicationContext(), "Contact Developer seleted", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
我最近做了类似这样的项目。