gpt4 book ai didi

android - 从 ListView 的自定义适配器中的 Intent 服务接收结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:44:32 24 4
gpt4 key购买 nike

我想做的是以下内容

  1. 我的自定义适配器布局中有一个按钮,它与服务器上的 php 脚本交互以在服务器上下载文件
  2. 下载过程是通过在后台调用IntentService来处理的
  3. 服务完成后,我想更新在 Activity 中启动的 ListView
  4. 我被 Receiver 类困住了

到目前为止我的代码如下:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
if(IsRepository){
view = vi.inflate(R.layout.filerepositorylayout, null);
}
}
BindLayoutElemnts(view, position);
return view;
}

这里是调用 Intent 服务的地方:

 private void BindLayoutElemnts(View view, int position) {
myFiles = (files) getItem(position);
img_Download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(getContext(),DownloadService.class);
intent.putExtra("FileID",""+myFiles.getID());
intent.putExtra("FileName",myFiles.getName());
context.startService(intent);
}
});
}

调用 Activity

这里是调用Receiver的地方

    public class user_repositry extends AppCompatActivity implements  DownloadReceiver.Receiver {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDownloadReceiver = new DownloadReceiver(new Handler());
myDownloadReceiver.setReceiver(this);
}

@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
Toast.makeText(user_repositry.this, "Downloaded", Toast.LENGTH_SHORT).show();

}

Intent 服务代码:

 ResultReceiver rec;
@Override
protected void onHandleIntent(Intent intent) {
rec = intent.getParcelableExtra("DownloadReceiver");
}

private void UpdateDBRecord() {
HashMap<String, String> PostData=new HashMap<String, String>();
PostData.put("call","UpdateFile");
PostData.put("ID", ""+file_ID);
BackgroundWorker registerWorker= new BackgroundWorker(this,this,PostData);
registerWorker.setShowLoadingMessage(false);
registerWorker.execute(Helper.getPhpHelperUrl());
}

这个函数就是上面registerWorkerPost execute中调用的函数

 @Override
public void processFinish(String results) {
Log.d("Download","Download DB updated");
Bundle bundle = new Bundle();
//bundle.putString("resultValue", "My Result Value. Passed in: " );
// Here we call send passing a resultCode and the bundle of extras
rec.send(Activity.RESULT_OK, bundle);
}

我现在被这个错误困住了:

Failed resolution of: Lcom/bassem/donateme/classes/DownloadReceiver;

我知道接收器没有被启动,但我似乎无法在我的逻辑中找到丢失的代码

感谢您的帮助

最佳答案

试试这个,

第 1 步: 在 Activity 中使用 ResultReceiver 创建结果处理程序

public ResultReceiver downloadReceiver = new ResultReceiver(new Handler()) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
//Broadcast /send the result code in intent service based on your logic(success/error) handle with switch
switch (resultCode) {
case 1: {
//Get the resultData and do your logic here
//Update your list view item here
break;
}
case 2: {
//Get the resultData and do your logic here
//Update your list view item here
break;
}
}
}
};

第二步:将result handler放入intent并启动intent service

Intent intent = new Intent(getContext(), DownloadService.class);
intent.putExtra("receiver",downloadReceiver);

第 3 步:在服务类中处理 Intent (获取结果处理程序的值)

final ResultReceiver receiver;

@Override
protected void onHandleIntent(Intent intent) {
receiver = intent.getParcelableExtra("receiver");
}

第 4 步:将数据发送/广播到结果处理程序

//based on your logic, change the result code and data

//Add your result data (success scenario)
Bundle bundle = new Bundle();
bundle.putString("result","Some text");
receiver.send(1,bundle);

//Add your result data (failure scenario)
receiver.send(0,bundle);

关于android - 从 ListView 的自定义适配器中的 Intent 服务接收结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39279931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com