Android button error - android

I made an app with three buttons. one makes a call, one sends an email and the third sends an sms. after making it the first time, i noticed the email button not responding. i tried to find an eroor but couldnt. so i switched the code so that the email button sends an sms and the sms button sends an email. once again the email button which is now supposed to send an sms, doesnt respond. any ideas?
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ContactDaveActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void buttonhandler(View view)
{
switch(view.getId())
{
case R.id.button1:
{
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:xxxxxxxxxx"));
startActivity(callIntent);
}
catch(ActivityNotFoundException activtyException)
{
Throwable e = null;
Log.e("dialingexample", "Call failed", e);
}
break;
}
case R.id.button2:
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
String s = "xyz#gmail.com";
i.putExtra(Intent.EXTRA_EMAIL, new String[]{s });
startActivity(Intent.createChooser(i, "Send mail..."));
break;
}
case R.id.button3:
{
String phoneNumber = "+xxxxxxxx";
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android -dir/mms-sms");
smsIntent.setData(Uri.parse("sms:"+phoneNumber));
startActivity(smsIntent);
break;
}
}
}
}

you have android:onClick="buttonhandler" in your main.xml file ?
and also check for all,button1,button2,button3
<Button android:id="#id/button1" android:onClick="buttonhandler" ... />
<Button android:id="#id/button2" android:onClick="buttonhandler" ... />
<Button android:id="#id/button3" android:onClick="buttonhandler" ... />

Related

Android BroadcastReceiver does not work phone calls

I was looking for examples.
My class look like:
<pre><code>
package com.example.pjimnez.samsung_auto_reply;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends AppCompatActivity {
//IntentFilter screenStateFilter;
Button btnStart;
TextView txtHome;
//PhoneState oPhoneState;
IntentFilter filter1;
private final BroadcastReceiver myPhoneState = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state == null) {
//Outgoing call
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, number, Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
//Log.e("tag", "EXTRA_STATE_OFFHOOK");
Toast.makeText(context, "EXTRA_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
//Log.e("tag", "EXTRA_STATE_IDLE");
Toast.makeText(context, "EXTRA_STATE_IDLE", Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
//Log.e("tag", "Incoming number : " + number);
Toast.makeText(context, number, Toast.LENGTH_LONG).show();
} else
Toast.makeText(context, "none", Toast.LENGTH_LONG).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filter1 = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
registerReceiver(this.myPhoneState, filter1);
btnStart = (Button) findViewById(R.id.btnStart);// Instancia del objeto boton intro
//No funciona en modo escucha
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtHome = (TextView) findViewById(R.id.txtHome);
txtHome.setText("Dio click en el boton!" );//cambio el contenido del TextView
//Envia el mensahe SMS
/*
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("5524234613", null, "Mensaje de prueba", null, null);
*/
}
});
}
}
enter code here
</code></pre>
My manifest has permissions to:
android.permission.READ_PHONE_STATE
android.permission.RECEIVE_SMS
android.permission.SEND_SMS
The problem is that my app never show the message on phone change state
i do not know whats is wrong with the BroadcastReceiver
Could you helpme
Try this:
1- add a class PhoneStateBroadcastReceiver which extends BroadcastReceiver and overwrite onReceive()
2- at runtime, request PHONE_STATE permission upfront
ActivityCompat.requestPermissions(myMainActivity,
new String[]{Manifest.permission.READ_PHONE_STATE},
READ_PHONE_STATE_CODE);
and give it via the system dialog
3- make a phone call
You'll see the intent caught in onReceive():
intent: Intent { act=android.intent.action.READ_PHONE_STATE flg=0x10 cmp=com.myApp.network.PhoneStateBroadcastReceiver (has extras) }
Action: android.intent.action.PHONE_STATE
Hope it helps

Android Button to open URL

I have two button which starts new intents, and they are working fine.
In the same activity i am now trying to make a new button that opens a URL link, but i cant make it work.
The two buttons starting new intents, are btn_Calender and btn_Info. So the new button which should open URL is btn_button4.
Can someone plese see my code and tell me what i am doing wrong.
Thank you all.
package com.xxxxxx;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.chrfugl.stubhuset.R;
public class HomeActivity extends Activity implements OnClickListener{
private Button btn_Calender, btn_Info, btn_button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btn_Calender = (Button)findViewById(R.id.btn_Calender);
btn_Info = (Button)findViewById(R.id.btn_Info);
btn_button4 = (Button)findViewById(R.id.button4);
btn_Calender.setOnClickListener(this);
btn_Info.setOnClickListener(this);
btn_button4.setOnClickListener(this);
}
public void onClick(View view) {
Intent intent;
switch (view.getId()) {
case R.id.btn_Calender:
intent = new Intent(this,MainActivity.class);
startActivity(intent);
break;
case R.id.btn_Info:
intent = new Intent(this,InfoActivity.class);
startActivity(intent);
break;
case R.id.button4:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
break;
}
}
}
Your switch statement in your onClick is wrong..
The ID of your button is R.id.button4:
btn_button4 = (Button)findViewById(R.id.button4);
In your onClick you are checking for the ID:
case R.id.btn_button4:
Which means you never reach your call.. The Intent itself is correct.
EDIT
Replace case R.id.btn_button4: with case R.id.button4: in your onClick-method

Android Dialer in the Background

Is it possible to call the Intent.ACTION_CALL in the background ?
I want my application to call but I don't want it to put in the background, I want it to stay in the foreground while it is calling.
The Intent.ACTION_CALL will call the default Android Phone activity and call the number you provided. If you want your App to be in the foreground, which i take to understanding you are builder your own calling app, you will have to implement the Calling part yourself.
Otherwise you can launch your activity with say a 5 seconds delay if you just want to show the user a context of your application.
You can invoke your activity after making a call using this,
package com.sdi.androidcall;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button call;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call = (Button) findViewById(R.id.call);
final PhoneCallListener phoneListener = new PhoneCallListener();
final TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
call.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
}, 10000);
}
});
}
private class PhoneCallListener extends PhoneStateListener {
boolean flag = true;
String LOG_TAG = "Call TEST";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
Log.i(LOG_TAG, "OFFHOOK");
// invoking activity
if (flag) {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
System.out.println("flagged retrieving app");
flag = false;
}
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(LOG_TAG, "IDLE");
Log.i(LOG_TAG, "restart app");
}
}
}
}
The activity will be called after a delay of 10 seconds here using PhoneStateListener
You need to specify the intent permissions in the mainfest as
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Try it, let me know if worked.

how to send email from input fields - my code debugging

i seem to have encountered a problem here.. i have the 2 edittext boxes and one button. when i click the button it gives me an option of what way to send the message, however it does not capture what my inputs are but gives out a weird message saying This is a Testandroid.widget.EditText#47b84299android.widget.EditText#47b8f0d9. Neither does it go to email and fill up the subject header.
this is my code.
package com.emailmetest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Activity1 extends Activity implements OnClickListener {
Button sendemail;
TextView input1, input2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendemail =(Button)findViewById(R.id.sendemail);
input1 = (TextView)findViewById(R.id.input1);
input2 = (TextView)findViewById(R.id.input2);
sendemail.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"test#hotmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "\nThis is a Test" + input1 + input2);
try {
startActivity(Intent.createChooser(i, "Send via..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this.getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}
try:
i.putExtra(Intent.EXTRA_TEXT , "\nThis is a Test" + input1.getText().toString() + input2.getText().toString());

Will big android Activity wiriting cause any effect to my App

I'm making my first official application to put on the market and i was wondering would over 120 new activities be too many if I'm only using them to pull text? If so is there a way I could turn MysecondActivity to a giant if or switch statement?
This is the example I found in a book I changed up case 0: I'm going to change up the rest be similar to case 0 once i get an answer.
Thank you in advance and sorry for messy code and writing
AndroidManifest.xml
<activity android:name=".MySecondActivity" />
ListActivityExample.java
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class ListActivityExample extends ListActivity{
static final String[] ACTIVITY_CHOICES = new String[] {
"Open new Actvity",
"Open Contacts",
"Open Phone Dialer Example",
"Search Google Example",
"Start Voice Command"
};
final String searchTerms = "superman";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3){
switch(arg2) {
case 0: //opens new screen
{Intent intent = new Intent(ListActivityExample.this,MySecondActivity.class);
startActivity(intent);
break;}
case 1: //opens phone dialer and fills in the given number
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("content://contacts/people/")));
break;}
case 2:
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("tel:12125551212")));
break;}
case 3: //
{
Intent intent1= new Intent(Intent.ACTION_WEB_SEARCH);
intent1.putExtra(SearchManager.QUERY, searchTerms);
startActivity(intent1);
break;}
case 4: //
{startActivity(new
Intent(Intent.ACTION_VOICE_COMMAND));
break;}
default: break;
}
}
});
}
}
MySecondActivity.java
mport android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MySecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("text here");
setContentView(tv);
}
}
There's nothing I can see about this code that will cause an issue with a large Activity stack. Start as many Activities as you like, as long as they aren't all stacked together at the same time. With this code you are starting a new Activity as a result of tapping an item in the list, but the user must finish the new Activity (press back) when returning to your list to start something else, so they aren't all in memory at once.
Does that help? Or did I misunderstand your question?

Categories

Resources