Unfortunately appName has stopped error - android

When I run my app following error message it gives:
Unfortunately appName has stopped error
I have following MainActivity.java file:
package com.example.hello_world;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnCheckedChangeListener {
Button btnSubmit,b2;
RadioButton rb1,rb2;
EditText etFirstName,etLastName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSubmit=(Button)findViewById(R.id.btnSubmit);
rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
btnSubmit.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnSubmit:
boolean didWork=true;
try
{
String fName=((EditText) findViewById(R.id.etFirstName)).getText().toString();
String LName=((EditText) findViewById(R.id.etLastNam)).getText().toString();
Toast msg1 = Toast.makeText(getBaseContext(),
fName, Toast.LENGTH_LONG);
msg1.show();
HNT entry=new HNT(MainActivity.this);
entry.open();
Toast msg2 = Toast.makeText(getBaseContext(),
fName, Toast.LENGTH_LONG);
msg2.show();
entry.createEntry(fName,LName);
entry.close();
break;
}
catch(Exception e)
{
didWork=false;
Toast msg1 = Toast.makeText(getBaseContext(),
"Catch Failed"+e.getMessage(), Toast.LENGTH_LONG);
msg1.show();
}
finally
{
if(didWork)
{
Toast msg1 = Toast.makeText(getBaseContext(),
"Sucess", Toast.LENGTH_LONG);
msg1.show();
}
else
{
Toast msg1 = Toast.makeText(getBaseContext(),
"Failed", Toast.LENGTH_LONG);
msg1.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 void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
}
}
xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/AppTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tvFName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="34dp"
android:text="First Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/etFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvFName"
android:layout_alignBottom="#+id/tvFName"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/tvFName"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etLastNam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/etFirstName"
android:layout_below="#+id/etFirstName"
android:layout_marginLeft="13dp"
android:layout_marginTop="43dp"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/etLastNam"
android:layout_alignLeft="#+id/tvFName"
android:text="Last Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/etLastNam"
android:layout_alignParentBottom="true"
android:layout_marginBottom="74dp"
android:text="Submit" />
</RelativeLayout>
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello_world"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hello_world.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>
</manifest>

There is no radio Button in the xml posted
But you have this
rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
You are probably getting NullPointerException. If you don't want remove the above.
Also remove
implements OnCheckedChangeListener
and this
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
}

Related

whenever i try to start the webview activity though a button it says the app has stopped

I am trying to create an app that implements the webview activity whenever you press a button but. Whenever I press it gives me an, unfortunately, message instead of going to it. Everything seems to be right and I put it through the debugger and it gave me the error "E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.time_app, PID: 7599
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.time_app/com.example.time_app.Browser}: android.view.InflateException: Binary XML file line #8: Error inflating class android.webkit.WebView".It says the problem is at line 42 in the browser.java file which is "setContentView(R.layout.activity_browser);".Ive checked all of my lines of code and it all seems right.
Browser.java
package com.example.time_app;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Calendar;
import java.util.Date;
public class Browser extends AppCompatActivity {
//creating the webview variable
WebView wb;
//the end variable takes in the finshed load time(it is a long function because of the number length
long end;
//this is just a varibale to be used for the alert code
final Context context = this;
//this is the total number converted as a string because the alert function does not take in float
String numberAsString;
//the start variable takes in the start load time(it is a long function because of the number length
long start;
//The total variable takes in the substraction of the end and start and is a float variale do to its shorter length
float total;
Date end_Time;
Date start_Time;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
wb = (WebView) findViewById(R.id.webview);
wb.setWebViewClient(new WebViewClient() {
//this function records the load time
public void onPageStarted(WebView view, String url, Bitmap favicon) {
start = System.currentTimeMillis();
start_Time = Calendar.getInstance().getTime();
}
//This function will take in the finished load time and create an alert
#RequiresApi(api = Build.VERSION_CODES.O)
public void onPageFinished(WebView view, String url) {
end = System.currentTimeMillis();
total=end-start;
end_Time = Calendar.getInstance().getTime();
// Total is the difference in milliseconds.
// Dividing by 1000, you convert it to seconds
numberAsString = String.valueOf(total/1000);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Load Time");
// set dialog message
alertDialogBuilder
.setMessage("Elapsed Time:"+numberAsString+"\n"+"Start Time:"+start_Time+"\n"+"EndTime:"+end_Time+"\n")
.setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
//declaring and setting the web setting variable
WebSettings webSettings = wb.getSettings();
//setting up the javascript to allow thw browser to use javascript
webSettings.setJavaScriptEnabled(true);
//loading url
wb.loadUrl("https://www.aa.com");
}
}
activity_browser.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".Browser">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout >
Manual_menu.java
package com.example.time_app;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Manual_menu extends AppCompatActivity {
public ListView lv;
public EditText nametxt;
public Button addbtn,updatebtn,deletebtn,submit;
public ArrayList<String> names=new ArrayList<String>();
public ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manual_menu);
lv=(ListView) findViewById(R.id.Listview1);
nametxt=(EditText) findViewById(R.id.nametxt);
addbtn=(Button) findViewById(R.id.addbtn);
updatebtn=(Button) findViewById(R.id.updatebtn);
deletebtn=(Button) findViewById(R.id.deletebtn);
submit=(Button) findViewById(R.id.Submit_btn);
//adapter
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,names);
lv.setAdapter(adapter);
//Set Selected Item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
nametxt.setText(names.get(i));
}
});
//add button event
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
add();
}
});
//update button event
updatebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
update();
}
});
//delete button event
deletebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
delete();
}
});
//clear button event
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openActivityDecision();
}
});
}
//add
private void add()
{
String name=nametxt.getText().toString();
if(!name.isEmpty() && name.length()>0)
{
//Add
adapter.add(name);
//Refresh
adapter.notifyDataSetChanged();
nametxt.setText("");
Toast.makeText(getApplicationContext(), "Added " +name,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "!!Nothing to Add " +name,Toast.LENGTH_SHORT).show();
}
}
//Update
private void update()
{
String name=nametxt.getText().toString();
int pos=lv.getCheckedItemPosition();
if(!name.isEmpty() && name.length()>0)
{
//Remove Item
adapter.remove(names.get(pos));
//insert
adapter.insert(name,pos);
//refresh
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Updated " +name,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "!!Nothing to Update " ,Toast.LENGTH_SHORT).show();
}
}
//delete
private void delete()
{
int pos=lv.getCheckedItemPosition();
if(pos > -1)
{
//remove
adapter.remove(names.get(pos));
//refresh
adapter.notifyDataSetChanged();
nametxt.setText("");
Toast.makeText(getApplicationContext(), "Deleted " ,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "!!Nothing to delete " ,Toast.LENGTH_SHORT).show();
}
}
//Submit
private void openActivityDecision()
{
Intent intent=new Intent(this, Browser.class);
startActivity(intent);
}
}
activity_manual_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Manual_menu">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:text="Name :"
/>
<EditText
android:id="#+id/nametxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView1"
android:ems="10"
/>
<ListView
android:id="#+id/Listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/addbtn"
android:choiceMode="singleChoice"
android:layout_marginTop="20dp">
</ListView>
<Button
android:id="#+id/addbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Listview1"
android:layout_below="#+id/nametxt"
android:layout_marginTop="19dp"
android:text="add" />
<Button
android:id="#+id/updatebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Listview1"
android:layout_toRightOf="#+id/addbtn"
android:layout_marginTop="19dp"
android:text="Update" />
<Button
android:id="#+id/Submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Listview1"
android:layout_alignParentRight="true"
android:layout_marginTop="19dp"
android:text="Submit" />
<Button
android:id="#+id/deletebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Listview1"
android:layout_toRightOf="#+id/updatebtn"
android:layout_marginTop="19dp"
android:text="Delete" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.time_app">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".File_menu"></activity>
<activity android:name=".Browser" />
<activity android:name=".file_load" />
<activity android:name=".Manual_menu" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Trouble in getting Location in Android SDK

I am new to Android, trying to implement Location Based Service but, I am getting errors. I am getting confused now.
Here are my files:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.om.locationdemo.MainActivity">
<Button
android:text="Get Location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/button" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="137dp"
android:id="#+id/button2" />
</RelativeLayout>
**AndroidManifest.xml**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.om.locationdemo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.om.locationdemo;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.test.mock.MockContentProvider;
import android.test.mock.MockContext;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b1, b2;
TextView tv;
LocationManager lmngr;
LocationListener llm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
tv = (TextView) findViewById(R.id.textView);
lmngr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
llm = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
tv.setText("Latitude = " + location.getLatitude() + " Longitude's = " + location.getLongitude());
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this, "Please Enable Location Service", Toast.LENGTH_LONG).show();
}
};
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
lmngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, llm);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
lmngr.requestLocationUpdates
(LocationManager.NETWORK_PROVIDER,0, 0, llm);
}
});
}
}

Changing ringer profile when receiving sms

Hello there i'm newbie in Android development
I'm trying to make an app that changes the ringer profile when receive a specific sms, also i can change it by the buttons on the layout (the buttons are working good), but the sms way isn't working
i tried as shown below
MainActivity.java
package com.example.test;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button Vibrate , Ring , Silent , Mode;
private TextView Status,sms;
public static AudioManager myAudioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Vibrate = (Button)findViewById(R.id.button2);
Ring = (Button)findViewById(R.id.button4);
Silent = (Button)findViewById(R.id.button3);
Mode = (Button)findViewById(R.id.button1);
Status = (TextView)findViewById(R.id.textView2);
sms = (TextView)findViewById(R.id.sms);
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
}
public void vibrate(View view){
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
public void ring(View view){
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
public void silent(View view){
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
public void mode(View view){
int mod = myAudioManager.getRingerMode();
if(mod == AudioManager.RINGER_MODE_NORMAL){
Status.setText("Current Status: Ring");
}
else if(mod == AudioManager.RINGER_MODE_SILENT){
Status.setText("Current Status: Silent");
}
else if(mod == AudioManager.RINGER_MODE_VIBRATE){
Status.setText("Current Status: Vibrate");
}
else{
}
}
public static void messageProcessing(String message) {
if(message=="ring")
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
else if (message=="vibrate")
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
else if(message=="silent")
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
#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;
}
}
RecieveSMS.java
package com.example.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.widget.Toast;
import android.os.Bundle;
public class RecieveSMS extends BroadcastReceiver
{
public static final String SMS_BUNDLE = "pdus";
#Override
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
MainActivity.messageProcessing(smsBody);
Toast.makeText(context, smsBody, Toast.LENGTH_SHORT).show();
}
}
}
}
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="#string/audio"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="144dp"
android:layout_marginLeft="40dp"
android:layout_toLeftOf="#+id/button2"
android:onClick="silent"
android:text="#string/Silent" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:onClick="ring"
android:text="#string/Ring" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button3"
android:layout_marginBottom="15dp"
android:onClick="mode"
android:text="#string/Mode" />
<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:layout_marginTop="46dp"
android:text="#string/Status"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:layout_alignRight="#+id/textView1"
android:onClick="vibrate"
android:text="#string/Vibrate" />
<TextView
android:id="#+id/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView1"
android:layout_marginTop="23dp"
android:text="#string/Sms"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="audio">Set Audio Profiles</string>
<string name="Ring">Ring</string>
<string name="Vibrate">Vibrate</string>
<string name="Silent">Silent</string>
<string name="Mode">Current Mode</string>
<string name="Status">Current Status</string>
<string name="Sms">SMS</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test.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>
<receiver android:name=".RecieveSMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
any help ??
also , well this app work in background or i should make a service to make that happen ?
I found out a way to solve my problem and that's how :
package com.example.test;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.widget.Toast;
import android.media.AudioManager;
import android.os.Bundle;
public class RecieveSMS extends BroadcastReceiver
{
public static final String SMS_BUNDLE = "pdus";
#Override
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
//Edited from here
if (smsBody.contains("silent"))
{
Intent in = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
12345, in, PendingIntent.FLAG_CANCEL_CURRENT);
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//To here
}
Toast.makeText(context, smsBody, Toast.LENGTH_SHORT).show();
}
}
}
}

my application stops working as soon as launched

(ANDROID GPS)
i made this application to send gps coordinates via sms but the app crashes(as soon as i open it) can somebody help me
logcat images
logcal image1
log cat image 2
MainActivity.java
package com.adzz.gps;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
import android.telephony.SmsManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
String m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager manager= (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener =new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
final String phoneNumber="9453603045";
double lat1=location.getLatitude();
Double d1= new Double(lat1);
double longi1=location.getLongitude();
Double d2=new Double(longi1);
m="latitude = "+ d1.toString() + "and latitude = "+ d2.toString();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, m, null, null);
TextView lat= (TextView) findViewById(R.id.lat);
TextView longi=(TextView) findViewById(R.id.longi);
lat.setText("latitude = "+lat1);
longi.setText("longitude ="+longi1);
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 25,
listener);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast toast=Toast.makeText(Main.this, m, 5000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.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;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/longi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<Button
android:id="#+id/button1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adzz.gps001"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.adzz.gps001.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>
</manifest>
In your manifest file you must include
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
Read the documentation under "Requesting User Permissions" for more details.
http://developer.android.com/guide/topics/location/strategies.html
Your app is failing because you need to add the android.permisssion.ACCESS_FINE_LOCATION permission to your manifest file.

application doesn't seem to do anything

(ANDROID GPS)
i made this application to send gps coordinates via sms but it doesn't seem to do any thing can anybody tell me what am i missing??
this is what logcat have to say on pressing the button
logcat image1
logcat image2
MainActivity.java
package com.adzz.gps;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
import android.telephony.SmsManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
String m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager manager= (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener =new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
final String phoneNumber="9453603045";
double lat1=location.getLatitude();
Double d1= new Double(lat1);
double longi1=location.getLongitude();
Double d2=new Double(longi1);
m="latitude = "+ d1.toString() + "and latitude = "+ d2.toString();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, m, null, null);
TextView lat= (TextView) findViewById(R.id.lat);
TextView longi=(TextView) findViewById(R.id.longi);
lat.setText("latitude = "+lat1);
longi.setText("longitude ="+longi1);
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 25,
listener);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast toast=Toast.makeText(Main.this, m, 5000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.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;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/longi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<Button
android:id="#+id/button1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adzz.gps001"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses- permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.adzz.gps001.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>
</manifest>
How about adding the permission for SMS in your AndroidManifest.xml?
<uses-permission android:name="android.permission.SEND_SMS" />

Categories

Resources