NfcAdapter.getDefaultAdapter returns null - java

I have 3 different devices with NFC. On 2 of them NfcAdapter.getDefaultAdapter(this) return what it should, but on third device returnsnull. There is app installed (not mine) on third device which works fine with NFC cards. The third device is SUNMI P1N-G. Any ideas?
Main Activity:
package com.test.nfctest;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private NfcAdapter nfcAdapter;
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView) findViewById(R.id.text1);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter==null) {
text1.setText("no nfc");
} else {
text1.setText("NFC");
}
}
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.nfctest">
<uses-permission android:name="android.permission.NFC"/>
<uses-feature android:name="android.hardware.nfc" />
<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>

There are several reasons for getting null adapter.
http://androidxref.com/6.0.1_r10/xref/frameworks/base/core/java/android/nfc/NfcAdapter.java#494
As per comments in NfcManager, there are two ways get the NFC Adapter:
Use {#link android.content.Context#getSystemService(java.lang.String)} with {#link Context#NFC_SERVICE} to create an {#link NfcManager}, then call {#link #getDefaultAdapter} to obtain the {#link NfcAdapter}.
NfcManager manager = (NfcManager) getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
You can just call the static helper {#link NfcAdapter#getDefaultAdapter(android.content.Context)}.
You can try the alternative method #1 described above.

Sorry for answering to old post.
On the Sunmi devices, at least on most of them, you need to use Sunmi Pay SDK to work with NFC and other cards.

Related

Android studio - some of the interpolator animations not working

I am trying to apply animations to my project, when I set the interpolator to:
alpha.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/bounce_interpolator">
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:duration="4000"
/>
</set>
SplashActivity.java:
package com.example.animationtesting;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
public class SplashActivity extends AppCompatActivity {
ImageView imageView;
TextView textView;
Animation alpha;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
imageView = findViewById(R.id.imageView);
textView = findViewById(R.id.textView);
alpha = AnimationUtils.loadAnimation(this,R.anim.alpha);
imageView.setAnimation(alpha);
textView.setAnimation(alpha);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
},4000);
}
}
Android Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AnimationTesting"
tools:targetApi="31">
<activity
android:name=".SplashActivity"
android:exported="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
The code is working fine when I set the interpolator as bounce_interpolator, accelerate_interpolator, anticipate_overshoot_interpolator but when I set the interpolator to other options like fade_in, fade_out,slide_out_right my app crashes. I don't get why this is happening.
First I thought your app crashed because you try to put the animation on two different xml-objects, but then I replicated the entire thing using this guide and for me, everything is working. (Although I did it using Kotlin).
I'm sorry, the only solution I could provide was this URL to the guide, but if you have any more questions, feel free to ask!

SMS Broadcast Receiver not working

Alright, I have tried every solution on Stack and nothing works.My current method registers the "SmsListener" receiver from the MainActivity. All I'm trying to do is initialize the onReceive method. There are no errors; It simply isn't picking up a broadcast. What am I doing wrong? Pasting the applicable code here. Anything else that may be needed please just ask.
Update:
Here is a similar unresolved issue
Listen Android incoming SMS when Google Hangout or other app receives it
I am testing under Android 6.0.1. Target Sdk version is 22. Min Sdk is 19. It's worth noting that I just tested my original code on an LG Optimus GPro with Android 4.4.2 and it worked. It still isn't working on my Nexus with Android 6.0.1.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.biapps.makin_biscuits">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<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" />
<action `android:name="android.service.notification.NotificationListenerService" />`
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ContactsList">
<intent-filter>
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
<receiver
android:name=".SmsListener"
android:priority="999"
android:enabled="true"
android:exported="true">
</receiver>
<receiver
android:name=".IncomingCallReceiver"
android:enabled="true"
android:exported="true">
</receiver>
</application>
Main Activity
package com.biapps.makin_biscuits;
import android.service.notification.NotificationListenerService;
import android.app.NotificationManager;
import android.content.Context;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//set object labels and states here
private ImageButton icon;
private AudioManager am;
private ImageButton people;
private ImageButton ring;
private NotificationManager nm;
private NotificationListenerService nls;
IncomingCallReceiver broadCastReceiver = new IncomingCallReceiver();
SmsListener smsReceiver = new SmsListener();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
icon = (ImageButton) findViewById(R.id.icon);
icon.setOnClickListener(imgButtonHandler);
people = (ImageButton) findViewById(R.id.people);
//people.setOnClickListener(peopleButtonHandler);
ring = (ImageButton) findViewById(R.id.ring);
}
int buttonstate = 0;
public View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
if (buttonstate == 0) {
icon.setImageResource(R.drawable.buttonup);
buttonstate = 1;
am.setRingerMode(0);
registerReceiver(broadCastReceiver, new IntentFilter(
"android.intent.action.PHONE_STATE"));
registerReceiver(smsReceiver, new IntentFilter(
"android.intent.action.DATA_SMS_RECEIVED"));
registerReceiver(smsReceiver, new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED"));
registerReceiver(smsReceiver, new IntentFilter(
"android.provider.Telephony.DATA_SMS_RECEIVED"));
Toast.makeText(getApplicationContext(),"Diving!", `Toast.LENGTH_SHORT)`
.show();
} else {
icon.setImageResource(R.drawable.button);
buttonstate = 0;
am.setRingerMode(2);
unregisterReceiver(broadCastReceiver);
unregisterReceiver(smsReceiver);
Toast.makeText(getApplicationContext(),"Surfacing!", Toast.LENGTH_SHORT)
.show();
}
}
};}
SmsListener
package com.biapps.makin_biscuits;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import android.telephony.TelephonyManager;
public class SmsListener extends BroadcastReceiver {
private static final String TAG = "SmsListener";
public static final String SMS_BUNDLE = "pdus";
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ON SMS RECEIVE BROADCAST", Toast.LENGTH_LONG).show();
Log.i(TAG, "SmsListener - onReceiveCalled");
}}
Try following way with highest reading priority value,
<receiver android:name=".SmsListener"
android:enabled="true"
android:exported="true"
android:permission="android.permission.READ_SMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
This will surely solve out your problem.
Update from below comment,
Since you are checking on Android version 6.0.1 just follow these steps,
Go to Settings,
Go to Apps
Select your Application
Choose Permissions Option
Enable SMS permission
After spending more than an hour I found that RECEIVE_SMS permission is required.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECEIVE_SMS},
MY_PERMISSIONS_REQUEST_SMS_RECEIVE);
Prioirty is not required to be set. This should work.
Found a solution.
First make another app your default SMS app.
Then: Google Hangout --> Settings (Disable merged conversations) --> SMS (Disable SMS)
You are registering broadcast your in your Activity, so it won't work if your app is in background. you can remove this from your Activity and you can register it in Manifest.
ex:
<receiver android:name=".SmsListener">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Along with this add permission to recieve sms.
Try this, and it will work

Cannot Launch an Activity on touching anywhere in Main Activity

Could please someone lend me a hand? I've been struggling with this problem for hours. I have a Main and Start activity, the Main is the Launcher with a bunch of information (TextViews), it should transit into Start activity as soon as someone touches the screen. I found some info on stack overflow on how to do this (setting the onTouchListener() on relative layout) however it does not work for me, so everytime I try with:
RelativeLayout layout= (RelativeLayout)findViewById(R.id.relativeLayout1);
layout.setOnTouchListener(this);
it tells me to cast 'this' to 'OnTouchListener' and here's the outcome:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView display1;
TextView display2;
TextView display3;
TextView display4;
ImageView image1;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_screen);
getActionBar().setTitle("AutoSMS");
RelativeLayout layout= (RelativeLayout)findViewById(R.id.relativeLayout1);
layout.setOnTouchListener((OnTouchListener) this);
display1 = (TextView) findViewById(R.id.textView1);
display2 = (TextView) findViewById(R.id.textView2);
display3 = (TextView) findViewById(R.id.textView3);
display4 = (TextView) findViewById(R.id.textView4);
image1 = (ImageView) findViewById(R.id.imageView2);
Intent openStarting = new Intent("android.intent.action.MAIN");
startActivity(openStarting);
}
}
Here's the Manifest, just in case
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.autosms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Start"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<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=".End"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.END" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
The DDMS gives me ClassCastException, MainActivity cannot be cast to ViewOnTouchListener
Any help would be GREATLY appreciated!
You can either implement your whole activity to be an onTouchListener or create a new onTouchListener when you add it. Like
public class myActivity extends Activity implements onTouchListener
or
thisObject.add(new onTouchListener(){
//TODO: Add code
});
There is no need to cast your Activity as an onTouchListener when it isn't. Casting is when you get the certain value of something if it's possible like if you have a double and you want to get it's value as an int. You just need to make a new onTouchListener or turn your activity into one because you don't have any.

How can remove the titlebar and remain the browser in fullscreen

I'm trying to create my first app, the browser for android with this code already done, but needed to adapt to my necessity. I still do not know java, and the examples I find I can not make it work.
I need help and reading.
How can remove the titlebar and remain the browser in fullscreen?
for later viewing and interacting with my application in jquery mobile
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AndroidMobileAppSampleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://www.");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tscolari.mobile_sample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AndroidMobileAppSampleActivity"
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>
1 - in your layout, set the WebView to match_parent in both layout_width and layout_height
2 - in your code, add these lines:
super.onCreate(savedInstanceState);
// Hiding Title bar of this activity screen
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
// Making this activity full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
in place of the existing
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
If your IDE is NOT SET to add the missing imports, just add the following line to your imports:
import android.view.Window;
import android.view.WindowManager;
update your manifeist like this :
only mentioning one thing:
android:theme="#android:style/Theme.NoTitleBar"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tscolari.mobile_sample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AndroidMobileAppSampleActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Unfortunately, app has stopped

Unfortunately, app has stopped. The purpose of the app is to display a web page when it is launched. I have researched this problem, and tried changing code in the manifest file. I have been unsuccessful at eliminating this message.
MainActivity.java
package com.example.testb1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
Intent getmobilepages = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.mcohio.org/government/auditor/mobile_app/home.cfm");
getmobilepages.setData(uri);
startActivity(getmobilepages);
}
}
testb1 Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testb1"
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.example.testb1.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.getmobilepages" />
</intent-filter>
</activity>
</application>
</manifest>
Edit: I tried running your code and was completely wrong. You are just missing the line:
super.onCreate(savedInstanceState);
At the start of your oncreate method. Add that in and it should compile just fine.
Make sure to post your logcat in the future, the error is identified clearly there.
You have not called super you will get SuperNotCalled Exception. You need to set you layout. Better to start another activity on button click.
Add internet permission in manifest
<uses-permission android:name="android.permission.INTERNET"/>
MainActivty
public class MainActivity extends Activity {
Button b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// call super
setContentView(R.layout.activity_main);
// set your layout
b = (Button) findViewById(R.id.button1);
//get button id
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// do something
Intent getmobilepages = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.mcohio.org/government/auditor/mobile_app/home.cfm");
getmobilepages.setData(uri);
startActivity(getmobilepages);
}
});
}
}

Categories