Android studio Location Manager problem, not sure how to resolve the issue - java

I have watched multiple tutorials and followed this one on Youtube: https://www.youtube.com/watch?v=2O_7V6aEjgM
I do not understand why the location is not being tracked. Here is some of my code:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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/Theme.TrackNet">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
--------------------------------------------------------------------------------------------
- above is the manifest file in my android studio project
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = location -> {
txtLatitude.setText(location.getLatitude() + "");
txtLongitude.setText(location.getLongitude() + "");
if(location.hasAccuracy()){
txtAccuracy.setText(location.getAccuracy() + "");
}
if(location.hasAltitude()){
txtElevation.setText(location.getAltitude() + "");
}
if(location.hasSpeed()){
txtSpeed.setText(location.getSpeed() + "");
}
radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
if(location.hasSpeed() && location.hasAltitude() && location.hasAccuracy()){
if(radioButton.getId() == R.id.switchLength){
building.addCoordinateLength(new Coordinate(location.getLatitude(),location.getLongitude(),location.getAltitude(),location.getAccuracy(),""));
}
else if(radioButton.getId() == R.id.switchWidth){
building.addCoordinateWidth(new Coordinate(location.getLatitude(),location.getLongitude(),location.getAltitude(),location.getAccuracy(),""));
}
}
else{
if(radioButton.getId() == R.id.switchLength){
building.addCoordinateLength(new Coordinate(location.getLatitude(),location.getLongitude(),0,0,""));
}
else if(radioButton.getId() == R.id.switchWidth){
building.addCoordinateWidth(new Coordinate(location.getLatitude(),location.getLongitude(),0,0,""));
}
}
Toast.makeText(getApplicationContext(),"Coordinate Recorded", Toast.LENGTH_SHORT).show();
};
above is some code on the onCreate method. I am using buttons to request location updates via a button

Related

Error While Sending Large Sentence As SMS using Array in java using default SMS service over SIM

I am having trouble on sending SMS from APP. I the App is working Fine on some Devices like Samsung, Nokia etc. But Not working on devices like OPPO, VIVO devices. All the devices I tested was SDK29 (ANDROID 10).
ps: I am newbie here
Below is my code Sombodey please Help me.
sendSms() method is Supposed to send the sms.
private void sendSms() {
String names = name_text.getText().toString();
String s1=phone_text.getText().toString();
String s2=("Hi "+names+", You have Successfully recharged ₹ " +d1 +" to your account on "+timeStamp+". Your current balance is ₹ "+amount+" . Kindly clear your balance on time without any due. \n - Thank you! ");
try {
if (!TextUtils.isEmpty(s1) && !TextUtils.isEmpty(s2)) {
if (checkPermission(Manifest.permission.SEND_SMS)) {
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> part = smsManager.divideMessage(s2); //this is line 629//
smsManager.sendMultipartTextMessage(s1, null, part, null, null);
} else {
Toast.makeText(UserDetailsActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(UserDetailsActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.wtf("userDetailsActivity","Error ", e);
e.printStackTrace();
}
}
nothing happens when this method is called on the device OPPO/VIVO
but works fine on samsung
What showing on the Run is
E/userDetailsActivity: Error
java.lang.SecurityException: getGroupIdLevel1
at android.os.Parcel.createException(Parcel.java:2085)
at android.os.Parcel.readException(Parcel.java:2053)
at android.os.Parcel.readException(Parcel.java:2001)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getGroupIdLevel1ForSubscriber(IPhoneSubInfo.java:1009)
at android.telephony.TelephonyManager.getGroupIdLevel1(TelephonyManager.java:4007)
at android.telephony.SmsMessage.hasEmsSupport(SmsMessage.java:1010)
at com.android.internal.telephony.SmsMessageBase.calcUnicodeEncodingDetails(SmsMessageBase.java:512)
at com.android.internal.telephony.gsm.SmsMessage.calculateLength(SmsMessage.java:1002)
at android.telephony.SmsMessage.fragmentText(SmsMessage.java:435)
at android.telephony.SmsManager.divideMessage(SmsManager.java:958)
at com.ancorp.aplo.activity.UserDetailsActivity.sendSms(UserDetailsActivity.java:629)
at com.ancorp.aplo.activity.UserDetailsActivity.access$700(UserDetailsActivity.java:61)
at com.ancorp.aplo.activity.UserDetailsActivity$11.onClick(UserDetailsActivity.java:607)
at android.view.View.performClick(View.java:7187)
at android.view.View.performClickInternal(View.java:7164)
at android.view.View.access$3500(View.java:813)
at android.view.View$PerformClick.run(View.java:27642)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:230)
at android.app.ActivityThread.main(ActivityThread.java:7752)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:508)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
this is my Manifest.xmml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ancorp.aplo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light">
<activity android:name=".activity.CollectionDetailsActivity"
android:parentActivityName=".activity.MainActivity"></activity>
<activity android:name=".activity.BoxDetailsActivity"
android:parentActivityName=".activity.MainActivity"/>
<activity android:name=".activity.PaymentDetailsActivity"
android:parentActivityName=".activity.MainActivity"/>
<activity
android:name=".activity.SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".activity.MainActivity" />
<activity android:name=".activity.AddPlanActivity" />
<activity
android:name=".activity.UserAddActivity"
android:label="Add Customer"
android:parentActivityName=".activity.MainActivity" />
<activity
android:name=".activity.UserDetailsActivity"
android:parentActivityName=".activity.MainActivity" />
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
<activity android:name=".activity.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE_PERMISSION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
I think the problem is with the way you declared the string.
Try without using the divide message function. Pass the arraylist directly to the sendMultipartTextMessage function

Android Permission ACCESS_FINE_LOCATION is always denied

I try to check the permission on button click and it always gives me -1
Here is my AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.natalia.assignment_2">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Also i have compileSdkVersion 28, minSdkVersion 22 and targetSdkVersion 28.
In my MainActivity, I try to check the permissions and it always denied, because it returns -1
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLocate = (Button) findViewById(R.id.buttonLocate);
buttonLocate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
askForPermission("android.permission.ACCESS_FINE_LOCATION");
});
}
private void askForPermission(String permission) {
if(MainActivity.this != null) {
if (PermissionChecker.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
// IT ALWAYS COMES TO THIS IF AND SHOWS -1
Toast.makeText(this, "" + String.valueOf(PermissionChecker.checkSelfPermission(MainActivity.this, permission)), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
}
}
}
}
I tried many hours, but nothing helps...what could be a problem here?
askForPermission("android.permission.ACCESS_FINE_LOCATION");
This is what you had when passing into your method.
Try doing :
askForPermission("Manifest.permission.ACCESS_FINE_LOCATION");
This may work. If not, I checked some other articles and everywhere I looked, it wasn't passed in as a String.
Try using this without a String:
(PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
Hope it works!

Unable to access getPackageManager GetActivityInfo from VideoDetailsFragment in Android Leanback app

I want to make an app for Android TV, so I am using the default Android TV activity in Android Studio as a template for my project, I want to add a button in the VideoDetailsFragment, this button only appears after the package manager detects that a secondary activity is active.
If the package manager detects my secondary activity is active and running it should show a button to disable it and if the activity is disabled it should show a button to enable it, but... the package manager can't get the name of the activity, it can't actually get the name of any package or activity, it always show NameNotFoundException both of my activities are android:name="android.intent.action.MAIN" in the Manifest because I want to add a launch intent for both from the launcher, but the secondary activity has the label android:enabled="false" because the button I want to put in the VideoDetailsFragment is supposed to enable and disable that activity.
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.software.leanback"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.Leanback">
<activity
android:name=".MainActivity"
android:banner="#mipmap/banner"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:logo="#mipmap/banner"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailsActivity" />
<activity android:name=".PlaybackOverlayActivity" />
<activity android:name=".BrowseErrorActivity" />
<activity android:name=".SecondaryActivity"
android:icon="#mipmap/ic_launcher"
android:banner="#mipmap/banner"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
</application>
Here is the VideoDetailsFragment code:
private void setupDetailsOverviewRow() {
Log.d(TAG, "doInBackground: " + mSelectedMovie.toString());
final DetailsOverviewRow row = new DetailsOverviewRow(mSelectedMovie);
row.setImageDrawable(getResources().getDrawable(R.drawable.default_background));
int width = Utils.convertDpToPixel(getActivity()
.getApplicationContext(), DETAIL_THUMB_WIDTH);
int height = Utils.convertDpToPixel(getActivity()
.getApplicationContext(), DETAIL_THUMB_HEIGHT);
Glide.with(getActivity())
.load(mSelectedMovie.getCardImageUrl())
.centerCrop()
.error(R.drawable.default_background)
.into(new SimpleTarget<GlideDrawable>(width, height) {
#Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable>
glideAnimation) {
Log.d(TAG, "details overview card image url ready: " + resource);
row.setImageDrawable(resource);
mAdapter.notifyArrayItemRangeChanged(0, mAdapter.size());
}
});
/******************************/
/*HERE IS THE PROBLEMATIC CODE*/
/******************************/
PackageManager pm = getActivity().getPackageManager();
ComponentName cn = new ComponentName(getActivity(), "com.valecast.myapp.SecondaryActivity");
ActivityInfo info = pm.getActivityInfo(cn, 0); //<===IT NEVER LOCATE THIS
if (info != null && info.enabled) {
// Component is enabled
row.addAction(new Action(ACTION_BUY, "it works"));
} else {
// Component is disabled
row.addAction(new Action(ACTION_BUY, "it doesn't"));
}
/******************************/
row.addAction(new Action(ACTION_WATCH_TRAILER, getResources().getString(
R.string.watch_trailer_1), getResources().getString(R.string.watch_trailer_2)));
row.addAction(new Action(ACTION_RENT, getResources().getString(R.string.rent_1),
getResources().getString(R.string.rent_2)));
row.addAction(new Action(ACTION_BUY, getResources().getString(R.string.buy_1),
getResources().getString(R.string.buy_2)));
mAdapter.add(row);
}
And here is a screenshot of the error show in android studio:
Here is another screenshot showing one of the suggested answer, but it doesn't work neither:
I wrapped that into a try catch, but when I run my app it always throw the NameNotFoundException in the Android Monitor.
What I am doing wrong?
You need to define the proper ComponentName:
new ComponentName("com.valecast.myapp", "com.valecast.myapp.SecondaryActivity");
and have a try catch:
try {
ActivityInfo info = pm.getActivityInfo(cn, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

Receiver in manifest file is giving error class or interface expected

There is an error in the manifest file that when I try to enter reciever it says class or interface expected
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.uzairaslam.silenter" >
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS_" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<receiver android:name="AlarmReceiver"/>
The receiver class which is not added is this
public class AlarmReceiver extends BroadcastReceiver {
SQLiteDatabase db;
String d ;
String deviceard;
String roomard ;
boolean status ;
public AlarmReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
d= intent.getStringExtra("DeviceId");
deviceard = intent.getStringExtra("Device_Ard_Id");
roomard = intent.getStringExtra("Room_Ard_id");
status = Boolean.parseBoolean(intent.getStringExtra("Device_Status"));
if(status)
Send_sms("y"+roomard+":"+deviceard+":oz" ,context);
else
Send_sms("y"+roomard+":"+deviceard+":fz" ,context);
Toast.makeText(context,"Mateen",Toast.LENGTH_LONG).show();
}
public void Send_sms(String msg,Context c)
{
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(c, alarmUri);
ringtone.play();
}
}
<receiver android:name="AlarmReceiver"/>
This line should be with in the application tag and give the fully qualified Receiver name. like below
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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="<receiver package>.AlarmReceiver"/>
</application>

android application working but crash after work done

my app is about turning phone on ringer mode via sms received but it crashes after its work is completed showing "Unfortunately, your app has stopped responding..". Here is my manifest and receiver.java file
My manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hide"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hide.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>
<activity
android:name="com.example.hide.Info"
android:label="#string/title_activity_info" >
<intent-filter>
<action android:name="com.example.hide.Info" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hide.Help"
android:label="#string/title_activity_help" >
<intent-filter>
<action android:name="com.example.hide.Help" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hide.Next"
android:label="#string/title_activity_next" >
<intent-filter>
<action android:name="com.example.hide.Next" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name="com.example.hide.MyReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.media.RINGER_MODE_CHANGED"/>
</intent-filter>
</receiver>
</application>
MYReceiver.java
public class MyReceiver extends BroadcastReceiver{
LocationManager lm;
LocationListener locationListener;
String sender;
#Override
public void onReceive(Context context, Intent intent) {
SmsMessage[] sms = null;
Bundle b = intent.getExtras();
String str = " SMS From ";
if (b != null) {
Object[] pdus = (Object[]) b.get("pdus");
sms = new SmsMessage[pdus.length];
for (int i = 0; i < sms.length; i++) {
sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (i == 0) {
sender += sms[i].getOriginatingAddress();
str += ":"+sms[i].getMessageBody().toString();
if (sms[i].getMessageBody().equals("Ring")) {
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_RING);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
am.setStreamVolume(AudioManager.STREAM_RING, maxVolume,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
}
}
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Log.d("Receiving", str);
}
}
}
Further more caused in log is showing NULLPOINTEREXCEPTION(), but i didn't get there is no code that throws exception.
But it is working fine as well it shows unfortunate message whenever i receive message on other app also.

Categories