I had tried this one to show my evdo dbm.
it could run on my emulator but couldnt on my real device, why?
is there somethinh wrong ?
this my source code :
package com.cdbmsof2;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class CdBmSOF2MainActivity extends Activity implements OnClickListener {
TextView dateAndTimeLabel;
private Button closeButton;
private int signalDBM = 0;
public class GetParams extends PhoneStateListener
{
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
signalDBM = signalStrength.getEvdoDbm();
}
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_cd_bm_sof2_main);
GetParams listener = new GetParams();
TelephonyManager TelManager = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
TelManager.listen(listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
Button btn=(Button)findViewById(R.id.start);
btn.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
Toast.makeText(this, "CDMA signal strength is " + this.signalDBM , Toast.LENGTH_SHORT).show();
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_cd_bm_sof2_main, menu);
return true;
}
}
this my manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cdbmsof2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".CdBmSOF2MainActivity"
android:label="#string/title_activity_cd_bm_sof2_main" >
<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.CHANGE_NETWORK_STATE" />
</manifest>
any one would help me please? best regards.
You may want to change the MODIFY permission into a READ permission as you're not modifying phone state, but trying to read it:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
I tried your code but the value of the signalDBM will not change. It will stay the same even if there is no internet connection at all. I am trying to do something similar and your code was of good help for me but now I face that problem. Does that program changes the values for you? Please let me know..
Related
I am trying to get the RSSI of the bluetooth connection. First i thought to display the RSSI of all nearby devices, so i checked the internet - got some information of the bluetooth classes and checked some examples. I tried a lot of thing, but with no success. I am at stage that I cant understand where exactly is the problem.
The target SDK is 29 and min SDK is 19. I am testing it on Android 10 (SDK 29)
MainActivity.java
package com.example.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BlendMode;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Set;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
BluetoothAdapter BA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
BA = BluetoothAdapter.getDefaultAdapter();
BA.startDiscovery();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private final BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
TextView rssi_msg = (TextView) findViewById(R.id.textView1);
rssi_msg.setText(rssi_msg.getText() + "" + rssi + "dBm\n");
}
}
};
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<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=".Main2Activity"></activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity" />
<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>
I found a "work around answer" but still can't do it properly. So the things that helped me and now it is working are: I made the targetSdkVersion to 26 (it was 29 before), minSdkVersion is still 19 (like before) and compileSdkVersion is still 29 (like before). This was in the gradle. And after I changed the target SDK I added this in my onCreate() method on the MainActivity
if (ActivityCompat.checkSelfPermission((Activity)this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity)this, new String[]{
android.Manifest.permission.ACCESS_COARSE_LOCATION
}, 10);
}
and now it is working on android 10, but I still can't do it for target SDK 29
I have external package class (my custom library), which I want to include in the project. It uses Activity, but shows error as you see.
If I place this command in main project class, it works well... What to do?
Here is the code:
package com.__MyDefaultLibrary;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.*;
import java.io.File;
import java.util.Calendar;
//
import android.app.Activity;
import com.oceanesa.samplevideorecorder.R;
public class __MyDefaultFunctions{
public android.app.Activity actv1 = android.app.Activity;
public void Initt(){
PreferenceManager.setDefaultValues(actv1.getBaseContext(), R.xml.mypreferences, false);
}
// ========== MY CUSTOM LIBRARY =============//
//button find
public Button fvb(int id) {
return (Button) actv1.findViewById(id);
}
//message show
public void msg(String text) {
Toast.makeText(actv1.getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
public View.OnClickListener optionsListener2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(actv1.getBaseContext(), com.__MyDefaultLibrary.__MyDefaultPreferencesInit1.class);
actv1.startActivity(i);
}
};
}
And AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oceanesa.samplevideorecorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
>
<activity
android:name="com.oceanesa.samplevideorecorder.VideoCaptureExample"
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.__MyDefaultLibrary.__MyDefaultPreferencesInit1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Update Answer just passing context to class
public class MyClass {
private static Context mContext;
private static Activity mActivity;
public MyClass(Context c) {
mContext= c;
}
public MyClass(Activity act) {
mActivity= act;
}
public static void showToastMethod() {
// then passing context or activity to
Toast.makeText(mContext, "mymessage ", Toast.LENGTH_SHORT).show();
// or Toast.makeText(mActivity, "mymessage ", Toast.LENGTH_SHORT).show();
}
}
Finally I got it working with the help of topic: How to display a Toast message in from a class that doesn't extend Activity
Problem was in my code. There was no need to call andorid.app at all. I didnt pass Activity to my external file, from where I couldnt call anything..
I had to pass activity like this (from main project file):
public MyExternalClass myEx = new MyExternalClass(Activity);
and needed some modifications...
I'm using intent method to make a simple app that takes you from main screen [with an enter button] to another screen which has three options. Code sourced online and seems to be error free, though my app crashes saying "Unfortunately [yourapp], has stopped" immediately after i press the button which is meant to take to the the other screen.
This is my first activity code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.enterBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, HomeActivity.class);
startActivity(intent);
}
});
}
}`
And this is my landing screen's activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class HomeActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
I'm really stuck with this issue and any help would be much appreciated. Many thanks in advance.
Every activity needs to be added in your manifest file under application tag. This seems to be the problem in your case.
Try to post you LogCat so that we might get some more information and if you have not yet added your Activity in the manifest, this is the way of adding it (Activities go under application tag)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.southmp3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/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>
<activity
android:name=".HomeActivity"
android:label="#string/app_name" >
</activity>
</application>
all the Activities should be going under the application tag
I had forgotten to register my new activity on the androidmanifest.xml file. That's what sorted my app crash issue.
my app force closes I do not know why. All it should do is vibrating when it is in the foreground and stop if it is not but when I start it on my phone it force closes. I added lines about vibrator usage into the manifest by hand. Maybe this is the problem. I have to anything else with it to be valid for the app?
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kevinboonemidi"
android:versionCode="1"
android:versionName="1.0" >
<permission android:name="android.permission.VIBRATE" ></permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.haptic.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>
My code:
package com.example.haptic_sof;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.example.kevinboonemidi.R;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity
{
protected Vibrator v = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long pwmCycles[];
pwmCycles = new long[4];
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = { 0, 200, 10 };
v.vibrate(pattern, 0);
}
#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;
}
protected void onPause(){
super.onPause();
v.cancel();
}
protected void onStop()
{
super.onStop();
v.cancel();
}
}
Your packages are a mess.
You define com.example.kevinboonemidi as your package in the manifest, declare your Activity to be in com.example.haptic and actually put your Activity in com.example.haptic_sof.
Change android:name="com.example.haptic.MainActivity" to android:name="com.example.haptic_sof.MainActivity" and your app should run, though you may need to end up using one package entirely at least to start with.
here
I uploaded my Android project made in Eclipse. The idea is that i have a service, which computes the sum of two random numbers. But when i press the OK Button, i don't see the result in that edit box... why? What i'm doing wrong? Please help
Thanks!
EDIT: The code:
//service class
package service;
import java.util.Random;
import com.android.AplicatieSuma;
import android.widget.EditText;
import android.widget.TextView;
public class ServiciuSuma
{
public ServiciuSuma() { }
public int CalculateSum()
{
Random generator=new Random();
int n=generator.nextInt();
int m=generator.nextInt();
return n+m;
}
}
The Application class:
package com.android;
import android.app.*;
import service.*;
public class ApplicationSum extends Application {
public ServiciuSuma service = new ServiciuSuma();
}
and the main Activity class:
package com.android;
import com.android.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class Activitate extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View btn_ok = findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
CalculeazaSuma();
}
});
}
private void CalculeazaSuma()
{
AplicatieSuma appState = ((AplicatieSuma)this.getApplication());
EditText txt_amount = (EditText)findViewById(R.id.txt_amount);
txt_amount.setText(appState.service.CalculateSum());
//BindData();
}
}
So that edit text does not show the sum of the random generated numbers, by the service. What's wrong?
Thanks
EDIT:
the manifest xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android"
android:versionCode="1"
android:versionName="1.0.1">
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true" />
<application android:name="ApplicationSum" android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true">
<activity android:label="#string/app_name" android:name=".Activitate">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
</manifest>
This fix will work:
txt_amount.setText(String.valueOf(appState.service.CalculateSum()));
Without that you pass integer and android thinks it's an id of a resource to display. You should really use DDMS to identify such problems.
Have you specified in manifest that you want ApplicationSum to be used as application class?
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:name="com.android.ApplicationSum">
...
</application>
</manifest>
Does this work?
public String CalculateSum()
{
Random generator=new Random();
int n=generator.nextInt();
int m=generator.nextInt();
String sum = Integer.toString(n+m) ;
Log.v("CalculateSum", "N+M = " + sum);
return sum;
}