【问题标题】:Custom intent crashing my app自定义意图使我的应用程序崩溃
【发布时间】:2017-07-01 20:33:27
【问题描述】:

我正在开发一个应用程序(对我自己来说只是一个有趣的项目,我不会把它放在 Play 商店中),它会发送一个祝酒词。我的最终目标是有一个 shell 命令来发送祝酒词。我不在乎我是如何得到这些结果的,我只是想让它起作用。

我想我会让 shell 命令向我的应用程序发送一个意图。我正在使用自定义意图:

<intent-filter>
    <action android:name="com.tylerr147.toast" />
<category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter> 

当我使用adb shell am start -a "com.tylerr147.toast" --es "android.intent.extra.TEXT" "toasty" -t "text/plain" 启动它时,我的应用崩溃了。

这是我的 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tylerr147.intenttoast" >
<application android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
    <activity android:name=".handler" >
        <intent-filter>
        <action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="com.tylerr147.toast" />
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
</manifest> 

还有我的 handler.java

package com.tylerr147.intenttoast;
import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.widget.*;
import java.util.*;
import android.util.*;

public class handler extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        handleSendText(intent);
    }
    public void handleSendText(Intent intent)
    {
        try{ String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            // Update UI to reflect text being shared
            Toast.makeText(getApplicationContext(), sharedText, Toast.LENGTH_LONG).show();
        }
        } catch(Exception e) {
    Log.e("Boked", e.toString());
        }
    }
} 

感谢您的帮助!

【问题讨论】:

    标签: android-intent android-toast


    【解决方案1】:

    试试这个

    adb shell am broadcast -a "com.tylerr147.toast" --es "android.intent.extra.TEXT" "toasty" -t "text/plain"
    

    而不是

    adb shell am start -a "com.tylerr147.toast" --es "android.intent.extra.TEXT" "toasty" -t "text/plain"
    

    用广播替换开始。

    希望它有效。

    【讨论】:

    • 虽然这次没有崩溃,但也没有干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    相关资源
    最近更新 更多