从BroadcastReceiver打开Async类

时间:2016-04-20 21:55:50

标签: java android android-asynctask broadcastreceiver

我知道这可能很简单,我见过很多人问过如何从另一个开放一个活动,但它似乎对我没用。我有一个类“NewInstallReceiver”,它扩展了BroadcastReceiver,另一个类“SendToClient”扩展了AsyncTask。我在NewInstallReceiver中创建了一个名为“applicationName”的字符串变量,我希望将其传递给SendToClient。

问题是我不确定我是否正确传递变量,因为我的SendToClient类似乎从未在第一时间打开。我知道这是因为我在Async类顶部的print语句永远不会在我的android监视器中打印。 如何从BroadcastReceiver类调用我的Async类并传递字符串变量?

NewInstallReceiver,它创建一个要传递给SendToClient的变量

public class NewInstallReceiver extends BroadcastReceiver
{


@Override
public void onReceive(Context context, Intent intent) {


    final PackageManager pm = context.getPackageManager();
    ApplicationInfo ai;
    try {


        ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);


    } catch (final PackageManager.NameNotFoundException e) {
        ai = null;
    }

    final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
    Log.d("Application NAME", "" + applicationName);

    // This line should open "SendToClient" and pass the variable "applicationName"

    new SendToClient().execute(applicationName);
}
}

我也尝试将这行代码添加到NewInstallReceiver中,但它也没有用。

Intent intent_2 = new Intent(context, SendToClient.class);
    context.getApplicationContext().startService(intent_2);

应该接收变量applicationName的SendToClient

public class SendToClient extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... params) {

    Log.d("Entered AsyncTask", "");

    String applicationName = params[0];

    return null;
}

}

0 个答案:

没有答案