我想从包含AsyncTask的通知中调用某些活动。当我单击通知时,它正在打开特定活动,但未执行异步的doInBackground。如果我直接调用该活动(不是从通知中调用),那么异步任务就可以正常工作。
这是我的服务类别(来自服务的呼叫通知)
package com.example.gajanand.simpletask;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MyService extends Service {
List<UserListResponse> userListResponse;
boolean datadownloded =false;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
FetchFromApi();
}
});
thread.start();
return START_STICKY;
}
private void sendNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), "notify_001");
Intent ii = new Intent(getApplicationContext(), RecyclerViewActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, ii, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText("Downloaded");
bigText.setBigContentTitle("Download");
bigText.setSummaryText("Status");
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Your Title");
mBuilder.setContentText("Your text");
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
mBuilder.setStyle(bigText).setDefaults(Notification.DEFAULT_ALL);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notify_001",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(0, mBuilder.build());
}
private void FetchFromApi() {
(Api.getClient().getUsersList()).enqueue(new Callback<List<UserListResponse>>() {
@Override
public void onResponse(Call<List<UserListResponse>> call, Response<List<UserListResponse>> response) {
Log.d("Gajanand", response.body().get(0).getName());
userListResponse = response.body();
// setDataInRecyclerView();
MyDb myDb=MyDb.getInstance(MyService.this);
datadownloded= myDb.insertUserDetails(userListResponse);
Log.d("Gajanand", "onResponse: "+datadownloded);
if(datadownloded){
sendNotification();
}
}
@Override
public void onFailure(Call<List<UserListResponse>> call, Throwable t) {
// if error occurs in network transaction then we can get the error in this method.
// Toast.makeText(GetActivity.this, t.toString(), Toast.LENGTH_LONG).show();
}
});
this.stopSelf();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
这是我的回收活动,我想从通知中调用。
public class RecyclerViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
FetchData fetchData = new FetchData();
fetchData.execute();
}
class FetchData extends AsyncTask<Void, Integer, Integer> {
ProgressDialog progress = new ProgressDialog(RecyclerViewActivity.this);
List<UserListResponse> userListResponse;
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.setMessage("Fetching data... ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
Log.d("Gajanand", "onPostExecute: " + integer);
progress.dismiss();
for (UserListResponse userListResponse : userListResponse) {
Log.d("Gajanand", "onPostExecute: " + userListResponse.getName());
}
}
@Override
protected Integer doInBackground(Void... voids) {
try {
MyDb myDb = MyDb.getInstance(RecyclerViewActivity.this);
userListResponse = myDb.fetchAlldata();
} catch (Exception e) {
Log.d("Gajanand", "doInBackground: " + e.toString());
}
return 0;
}
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gajanand.simpletask">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
<activity android:name=".RecyclerViewActivity"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>
</application>
</manifest>
我将从主要活动中开始服务。
谁能告诉我?为什么从通知异步无法正常工作?