小编Mik*_* M.的帖子

警告:此AsyncTask类应该是静态的,否则可能会发生泄漏

我在我的代码中收到警告:

这个AsyncTask类应该是静态的,否则可能会发生泄漏(匿名android.os.AsyncTask)

完整的警告是:

此AsyncTask类应该是静态的,否则可能会发生泄漏(匿名android.os.AsyncTask)静态字段将泄漏上下文.非静态内部类具有对其外部类的隐式引用.如果该外部类例如是片段或活动,则此引用意味着长时间运行的处理程序/加载器/任务将保留对活动的引用,从而阻止其收集垃圾.同样,对来自这些较长时间运行的实例的活动和片段的直接字段引用可能会导致泄漏.ViewModel类绝不应指向视图或非应用程序上下文.

这是我的代码:

 new AsyncTask<Void,Void,Void>(){

        @Override
        protected Void doInBackground(Void... params) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mAdapter.notifyDataSetChanged();
                }
            });

            return null;
        }
    }.execute();
Run Code Online (Sandbox Code Playgroud)

我该如何纠正?

android android-asynctask android-runonuithread

232
推荐指数
3
解决办法
7万
查看次数

深层链接和多个应用实例

我在我的应用中实现了深层链接.我在清单文件中添加了此intent过滤器,并且深层链接正在运行.

<intent-filter>
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
    <category android:name="android.intent.category.VIEW" /> 
    <data
        android:host="www.mywebsite.com"
        android:pathPrefix="/something"
        android:scheme="http" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

问题是通过深度链接,我的应用程序将在当前应用程序之上启动.如果我在Gmail中并点击了一个链接,那么我的应用就会在Gmail上启动.我想以不同方式启动我的应用程序.

如果我的应用已经在后台运行,我点击Gmail中的一个重定向到我的应用的链接,我将同时运行两个应用实例; 一个在后台,另一个在Gmail之上.我想一次只运行我的应用程序的一个实例,因此它不在当前应用程序(Gmail)之上.我怎样才能做到这一点?

android deep-linking

48
推荐指数
3
解决办法
2万
查看次数

使用SMS验证设备的电话号码

我试图通过让设备向自己发送短信来验证Android设备的电话号码,并自动检查是否已收到短信.我怎样才能做到这一点?

sms android

26
推荐指数
1
解决办法
1万
查看次数

android默认制作默认短信应用程序

所以我在这里遵循这个教程,将我的短信设置为默认,但由于某种原因,我的代码无效.我试图尽可能多地查看,但所有内容都指向同一个教程或已经过时.我也需要接收器吗?谁能解释我做错了什么?

代码:

@Override
protected void onResume()
{
    super.onResume();
    Log.i("MainAcitvity", "On Resume Called");
    // Only do these checks/changes on KitKat+, the "mSetDefaultSmsLayout" has its visibility
    // set to "gone" in the xml layout so it won't show at all on earlier Android versions.
    final String myPackageName = getPackageName();

    if (Utility.hasKitKat())
    {
        if (Utility.isDefaultSmsApp(this))
        {
            // This app is the default, remove the "make this app the default" layout and
            // enable message sending components.
            mSetDefaultSmsLayout.setVisibility(View.GONE);
        }
        else
        {
            Log.i("MainActivity", …
Run Code Online (Sandbox Code Playgroud)

sms android telephony default

16
推荐指数
1
解决办法
2万
查看次数

findViewById()为对话框中的视图返回null

问题是,无论我在何处或如何调用此布局的组件,它们始终返回null.

setView(inflater.inflate(R.layout.search_layout, null))
Run Code Online (Sandbox Code Playgroud)

这很好用.它显示了里面的布局Dialog,但是,子节点总是返回为null findViewById(R.id.some_search_layout_children).

我试图清理我的项目多次,试图实现另一个类我Dialog,叫findViewById()我的主要成员Activity,内部initSearch()方法,以及一个匿名实现内部OnClickListenerDialog,但都具有相同的结果.我还尝试将孩子们分成独立的Views并以编程方式调用它们:

TextView text = (TextView) findResourceById(R.id.new_independant_textview);
Run Code Online (Sandbox Code Playgroud)

但是,同样的结果.

这是相关代码:

public class Xyz extends Activity {
    public void onCreate(...) { // some listener will trigger initSearch() }

    private void initSearch() {
        AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        searchDialog.setTitle("Search Photos");
        searchDialog.setMessage("Specify tag and value...");
        // R.layout.search_dialog is my custom layour, it displays fine, it works. 
        searchDialog.setView(inflater.inflate(R.layout.search_dialog, null));
        EditText …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception android-dialog findviewbyid android-resources

12
推荐指数
1
解决办法
3万
查看次数

如何淡出TextView中最后一行的结尾?

我们如何在最后一行实现淡出效果TextView,例如Play商店应用中的"WHAT'NE NEW"部分?

Play商店应用的屏幕截图

android textview android-layout

9
推荐指数
1
解决办法
969
查看次数

在滑动时移动和调整DrawerLayout的内容

我刚看到这个应用程序,并看到了这个自定义动画DrawerLayout.我猜它必须首先采取截图,然后绘制一个自定义ViewActivity,但我不知道这件事,也没有细节.有谁知道如何做到这一点?

在此输入图像描述

android drawerlayout

8
推荐指数
1
解决办法
3560
查看次数

使用findViewById()找不到视图

即使ID确实存在,我也无法TextView通过呼叫找到findViewById().

OtherActivity:

public class OtherActivity extends Activity { 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TextView textView = (TextView)findViewById(R.ID.txt02);
        super.onCreate(savedInstanceState);//line 5
        setContentView(R.layout.other_activity);
        //textView.setText(ACCESSIBILITY_SERVICE);
        Button button = (Button)findViewById(R.ID.otherActivity_Btn);
        button.setText(R.string.otherActivityBtn);
        button.setOnClickListener(new GoBackBtnListener(this));
    }

    class GoBackBtnListener implements OnClickListener {
        OtherActivity other = null;
        public GoBackBtnListener(OtherActivity p_other) {
            other = p_other;
        }
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(other, Activity01Activity.class);
            other.startActivity(intent);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在第5行添加了断点,并以调试模式启动了项目.当它在断点处停止时,我将鼠标移动到变量textView,它为空.

other_activity布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-view findviewbyid

5
推荐指数
1
解决办法
3万
查看次数

测量SMS发送速度

我需要测量Android设备中SMS的发送速度.我正在考虑以下方法.请告诉我它是否正确,或者是否有更好的解决方案.

计算发送10条消息所花费的时间,并计算发送每条消息所花费的平均时间.但这里的速度在哪里?字符数会增加花费的时间吗?

这是我写的代码.

    SmsManager smsManager = SmsManager.getDefault();
    long startTime = System.currentTimeMillis();

    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);

    long endTime = System.currentTimeMillis();

    long result = endTime - startTime;
Run Code Online (Sandbox Code Playgroud)

result是742毫秒,但我怀疑这是正确的.这意味着每条SMS只需要74毫秒.

android

5
推荐指数
1
解决办法
358
查看次数

如何更改 DatePickerDialog 中 NumberPickers 的顺序

在此处输入图片说明

我想交换月份和日期(微调器)。我想将日期列 (Spinner) 发送到左侧,将月份列 (Spinner) 发送到中间。是否可以?

android

4
推荐指数
2
解决办法
1691
查看次数