Skip to content

Commit

Permalink
add NFCApp/res
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjobs committed Mar 31, 2020
1 parent a580a2b commit a7b0d7d
Show file tree
Hide file tree
Showing 27 changed files with 502 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.edu.jssvc.nfcapp;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("cn.edu.jssvc.nfcapp", appContext.getPackageName());
}
}
25 changes: 25 additions & 0 deletions NFCApp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.jssvc.nfcapp">

<uses-permission android:name="android.permission.NFC"/>

<application
android:allowBackup="true"
android:icon="@mipmap/logo2"
android:label="@string/app_name"
android:roundIcon="@mipmap/logo2"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InstalledAppListActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cn.edu.jssvc.nfcapp;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.List;

public class InstalledAppListActivity extends ListActivity implements
AdapterView.OnItemClickListener {
private List<String> mPackages = new ArrayList<>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

PackageManager packageManager = getPackageManager();
List<PackageInfo> packageInfos = packageManager
.getInstalledPackages(PackageManager.GET_ACTIVITIES);
for (PackageInfo packageInfo : packageInfos) {
mPackages.add(packageInfo.applicationInfo.loadLabel(packageManager)
+ "\n" + packageInfo.packageName);
}

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
mPackages);
setListAdapter(arrayAdapter);
getListView().setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent();
intent.putExtra("package_name", mPackages.get(position));
setResult(1, intent);
finish();
}

}
116 changes: 116 additions & 0 deletions NFCApp/app/src/main/java/cn/edu/jssvc/nfcapp/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cn.edu.jssvc.nfcapp;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private Button button;
private String mPackageName;
private NfcAdapter mNfcAdapter;
private PendingIntent mPendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = findViewById(R.id.button);

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()), 0);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), InstalledAppListActivity.class);
startActivityForResult(intent, 0);
}
});

}

@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (mPackageName == null)
return;

Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

writeNFCTag(detectedTag);
}

@Override
public void onResume() {
super.onResume();

if (mNfcAdapter != null)
//拦截其他NFC应用
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
null);
}

@Override
public void onPause() {
super.onPause();

if (mNfcAdapter != null)
//取消拦截
mNfcAdapter.disableForegroundDispatch(this);
}

public void writeNFCTag(Tag tag) {
if (tag == null) {
return;
}
NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[]{NdefRecord
.createApplicationRecord(mPackageName)});
int size = ndefMessage.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();

if (!ndef.isWritable()) {
return;
}
if (ndef.getMaxSize() < size) {
return;
}
ndef.writeNdefMessage(ndefMessage);
Toast.makeText(this, "写入成功", Toast.LENGTH_LONG).show();
}

} catch (Exception e) {
e.printStackTrace();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 1) {
button.setText(data.getExtras().getString(
"package_name"));
String temp = button.getText().toString();
mPackageName = temp.substring(temp.indexOf("\n") + 1);
}

}


}
34 changes: 34 additions & 0 deletions NFCApp/app/src/main/res/drawable-v24/ic_launcher_foreground.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeWidth="1"
android:strokeColor="#00000000">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
Loading

0 comments on commit a7b0d7d

Please sign in to comment.