m0_47028453
2021-06-22 19:59
采纳率: 66.7%
浏览 38

intent类不会用,写的是NFC读卡软件

package com.example.nfc;

import androidx.appcompat.app.AppCompatActivity;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.InstallSourceInfo;
import android.nfc.FormatException;
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 java.io.IOException;

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(String.valueOf(getClass())),0);

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

    }

    public void onNewIntent(Intent intent) {

        super.onNewIntent(intent);
        if(mPackageName == null){
            return;
        }

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

        writeNFCTag(detectedTag);


    }

    private 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();
        }
    }

    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);
        }
    }


}

用的是Android Studio软件,写安卓程序这两个类不会用,咋用啊各位

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 帅次 2021-06-22 20:28
    已采纳

    在外面不方便改代码。

    你可以去我这篇文章找答案,这里面都有我用#CSDN#这个app发现了有技术含量的博客,小伙伴们求同去《Android-面试题Service详解》, 一起来围观吧 https://blog.csdn.net/g984160547/article/details/118051937?utm_source=app&app_version=4.9.1&code=app_1562916241&uLinkId=usr1mkqgl919blen

    如有帮助请采纳。

    已采纳该答案
    评论
    解决 1 无用
    打赏 举报

相关推荐 更多相似问题