我在android中有一个通知,我想做类似的事情
当我单击通知中的MyStaticClass.start()时,该类不是活动,仅用于实例化所需的类。
我该怎么办?
这是我的实际代码:
String notificationContent = "Hay una nueva version disponible";
String notificationTitle = "Firext";
Bitmap largeIcon = BitmapFactory.decodeResource(FirextApplication.getInstance().getResources(), R.drawable.ic_launcher);
int smalIcon = R.drawable.ic_launcher;
Intent intent = new Intent(FirextApplication.getInstance(), UpdateProcess.class);
intent.setData(Uri.parse("content://" + Calendar.getInstance().getTimeInMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(FirextApplication.getInstance(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager notificationManager = (NotificationManager) FirextApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
FirextApplication.getInstance())
.setWhen(Calendar.getInstance().getTimeInMillis())
.setContentText(notificationContent)
.setContentTitle(notificationTitle)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(notificationTitle)
.setLargeIcon(largeIcon)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
Notification notification = notificationBuilder.build();
notificationManager.notify((int) Calendar.getInstance().getTimeInMillis(), notification);
我需要打的课
public class UpdateProcess {
private static final String APPLICATION_VND_ANDROID_PACKAGE_ARCHIVE = "application/vnd.android.package-archive";
private static final String NEWVERSION = "NewVersion.apk";
private static final File EX = Environment.getExternalStorageDirectory();
private static volatile UpdateProcess instance = null;
private Activity activity;
private UpdateProcess(Activity activity) {
this.activity = activity;
checkForNewUpdates();
}
public static void startUpdate(Activity activity) {
if (instance == null) {
synchronized (UpdateProcess.class) {
if (instance == null) {
instance = new UpdateProcess(activity);
}
}
} else {
UpdateProcess.instance.activity = activity;
}
}
我只需要做一个UpdateProcess.startUpdate();
编辑:
这是我的新代码:
private void showNot() {
String notificationContent = "Hay una nueva version disponible";
String notificationTitle = "Firext";
Bitmap largeIcon = BitmapFactory.decodeResource(FirextApplication.getInstance().getResources(), R.drawable.ic_launcher);
int smalIcon = R.drawable.ic_launcher;
Intent intent = new Intent(FirextApplication.getInstance(), UpdateProcess.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
Notification notificationBuilder = new NotificationCompat.Builder(context)
.setWhen(Calendar.getInstance().getTimeInMillis())
.setContentText(notificationContent)
.setContentTitle(notificationTitle)
.setSmallIcon(smalIcon)
.setTicker(notificationTitle)
.setLargeIcon(largeIcon)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
//.setContentIntent(pendingIntent)
.addAction(android.R.drawable.arrow_up_float, "Call", pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder);
}
public class UpdateProcess extends IntentService {
private static final String APPLICATION_VND_ANDROID_PACKAGE_ARCHIVE = "application/vnd.android.package-archive";
private static final String NEWVERSION = "NewVersion.apk";
private static final File EX = Environment.getExternalStorageDirectory();
private static volatile UpdateProcess instance = null;
private Context context;
private UpdateProcess(Context context) {
super("UpdateProcess");
this.context = context;
checkForNewUpdates();
}
@Override
protected void onHandleIntent(Intent intent) {
startUpdate(getApplicationContext());
}
但是当我点击动作时,什么也没有发生..我得到了这个错误:
05-10 17:15:22.955 753-7617 /? W / ActivityManager:无法启动服务意图{flg = 0x10000000 cmp = com.firext.android / .util.UpdateProcess bnds = [192,346] [1080,490]} U = 0:找不到
编辑2。现在工作,我忘了在清单中包含服务
最佳答案
我需要在UpdateProcess中的方法中显示一条吐司消息,我应该使用什么?
您的Notification
可以通过其Activity
触发Service
,BroadcastReceiver
或PendingIntent
。 Activity
是Context
。 Service
是Context
。 BroadcastReceiver
接收Context
作为onReceive()
的参数。因此,在所有三个位置中,您已经有一个Context
,因此请使用该Toast
。
您可能还考虑完全摆脱Toast
。 checkForNewUpdates()
仅适用于用户不需要的消息,因为用户可能看不到它。
我只需要做一个UpdateProcess.startUpdate();
看来UpdateProcess
将做一些重要的工作。
如果该工作少于15秒,请将UpdateIntentService
转换为IntentService
,扩展checkForNewUpdates()
,然后在onHandleIntent()
中进行您的getService()
工作。您可以使用PendingIntent
UpdateIntentService
触发WakefulBroadcastReceiver
进行工作。
如果工作可能需要更长的时间,则应考虑使用the WakefulIntentService
pattern(或也许是my ),以确保设备保持足够长的唤醒时间以完成工作。