0

我正在尝试开始为 Tasker 应用程序创建 Android 插件。我首先下载了“toast”示例并阅读了文档。我还在玩这个例子,但是当我注释掉 toast 动作并用代码替换它来创建一个警报对话框(我在许多其他应用程序中做过)时,当我在 Tasker 中尝试它时,tasker 插件总是崩溃. 我正在使用安卓工作室。

有谁知道为什么?我的代码如下:

public class FireReceiver extends BroadcastReceiver
    {

        /**
         * @param context {@inheritDoc}.
         * @param intent the incoming {@link com.twofortyfouram.locale.Intent#ACTION_FIRE_SETTING} Intent. This
         *            should contain the {@link com.twofortyfouram.locale.Intent#EXTRA_BUNDLE} that was saved by
         *            {@link EditActivity} and later broadcast by Locale.
         */
        @Override
        public void onReceive(final Context context, final Intent intent)
        {
    /*
     * Always be strict on input parameters! A malicious third-party app could send a malformed Intent.
     */

            if (!com.twofortyfouram.locale.Intent.ACTION_FIRE_SETTING.equals(intent.getAction()))
            {
                if (Constants.IS_LOGGABLE)
                {
                    Log.e(Constants.LOG_TAG,
                            String.format(Locale.US, "Received unexpected Intent action %s", intent.getAction())); //$NON-NLS-1$
                }
                return;
            }
    BundleScrubber.scrub(intent);

    final Bundle bundle = intent.getBundleExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);
    BundleScrubber.scrub(bundle);

    if (PluginBundleManager.isBundleValid(bundle))
    {
        final String message = bundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
        //Toast.makeText(context, message, Toast.LENGTH_LONG).show();

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setMessage("Yes, this worked!")
                .setIcon(R.drawable.ic_launcher)
                .setTitle(R.string.dialog_title);

        builder.setPositiveButton(R.string.dialog_yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User clicked OK button
                //Toast.makeText(context, "You clicked yes", Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton(R.string.dialog_no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
                //Toast.makeText(getApplicationContext(), "You clicked no", Toast.LENGTH_LONG).show();
            }
        });


        AlertDialog dialog = builder.create();
        dialog.show();

    }

}
4

1 回答 1

0

对于任何可能遇到此问题的人,我最终解决了这个问题。基本上 FireReceiver 无法显示警报等内容,因此您必须使用 FireReceiver 本质上打开另一个活动,而不是然后处理警报或您想要显示的任何其他内容。

这对我有用(我已经更改了包名,但你明白了要点)。NewActiviy 是您希望 FireReciever 打开的任何活动。然后它会向您展示我的代码,用于传递变量,这是非常标准的:

Intent openNext = new Intent();
        openNext.setClassName("com.yourcompany.yoursetting", "com.yourcompany.blahblah.receiver.NewActivity");

        openNext.putExtra("AlertTitle", message);
        openNext.putExtra("AlertContentsString", AlertContents);
            openNext.putExtra("BackColour", BackgroundC);
            openNext.putExtra("TextColour", TitleC);

            openNext.putStringArrayListExtra("ShowArray", arr);

        openNext.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(openNext);
于 2016-04-27T07:24:24.783 回答