我最近编写了一个Android Widget并在模拟器和我的Galaxy S上进行了测试,它在两者上都运行良好,在我发布到Android市场后,我现在收到一些错误报告.
我在Widget类的onUpdate中声明了一个服务,如下所示:
if (appWidgetIds != null) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to
// this
// provider
for (int i = 0; i < N; i++) {
// Start Service for each instance
int appWidgetId = appWidgetIds[i];
Intent active = new Intent(context, DialerService.class);
active.setAction("Start");
active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
context.startService(active);
}
}
Run Code Online (Sandbox Code Playgroud)
有些人得到的错误是:
java.lang.RuntimeException: Unable to start service dialer.impact.DialerService@45f926f0 with null: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3282)
at android.app.ActivityThread.access$3600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211)
at android.os.Handler.dispatchMessage(Handler.java:99) …Run Code Online (Sandbox Code Playgroud) 我正在制作一个应用程序,允许用户拍照并通过电子邮件自动发送到他选择的电子邮件.到目前为止,我能够拍照并存储在SD卡中.现在我只需要从文件夹(/ sdcard/Pictures/PhotoSender /)获取图片的功能,并自动发送到用户请求的电子邮件.我怎样才能做到这一点?
我在那个文件夹中有图片.我只需要一些函数来生成电子邮件,将图片(.jpg)作为附件发送(所有这些都在背景上).当电子邮件完全发送时,应该弹出一个"上传完成"的Toast.同时用户应该可以自由拍摄更多照片,因此上传请求应该放在队列中.用户不应使用他的电子邮件帐户登录发送.如果需要,我可以为我的应用创建一个电子邮件帐户作为"发件人".请帮我!
email android email-attachments android-intent android-service
在我的应用程序中,我将我的服务放在前台,以防止它被使用:
startForeground(NOTIFY_ID, notification);
Run Code Online (Sandbox Code Playgroud)
这也向用户显示通知(这很棒).问题是以后我需要更新通知.所以我使用代码:
notification.setLatestEventInfo(getApplicationContext(), someString, someOtherString, contentIntent);
mNotificationManager.notify(NOTIFY_ID, notification);
Run Code Online (Sandbox Code Playgroud)
接下来的问题是:这样做是否会使服务脱离其特殊的前景状态?
在这个答案中,CommonsWare表明这种行为是可行的,但他不确定.那么有谁知道实际答案?
注意:我知道解决这个问题的一个简单方法是startForeground()每次我想要更新通知时重复调用.我想知道这种替代方案是否也有效.
我有一个正在运行和监听麦克风输入的Android服务.我希望它在满足某个标准时启动活动.为了创建一个Intent,我需要应用程序上下文.我怎么才能得到它?
Intent i = new Intent(ctx, SONR.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
Run Code Online (Sandbox Code Playgroud)
以上行不会启动我的活动.
这是我的构造函数
public SONRClient(Context c, AudioRecord ar, int buffsize, final AudioManager am) {
theAudioManager = am;
theaudiorecord = ar;
bufferSize = buffsize;
ctx = c;
CLIENT_ON = true;
}
Run Code Online (Sandbox Code Playgroud)
这是我的onCreate
@Override
public void onCreate() {
try {
// LogFile.MakeLog("\n\nSONRClient CREATED");
clientStopReceiver = new StopReceiver();
ctx.registerReceiver(clientStopReceiver,
new IntentFilter(SONR.DISCONNECT_ACTION));
myByteReceiver = new SONRByteReceiver();
theListener = new MicSerialListener(
theaudiorecord, bufferSize, myByteReceiver);
theApplication = getApplication();
} catch (Exception e) {
e.printStackTrace();
ErrorReporter.getInstance().handleException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
myByteReceiver …
java android android-intent android-service android-activity
我想检查服务是否正在运行l写了这段代码
public class startServiceOrNo {
public static void startServiceIfItsNotRuning(Class<?> class1, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (class1.getName().equals(service.service.getClassName())) {
Lg.d("servisstart SERVICE ALREADY START" + class1.getName());
return;
}
}
Lg.d("servisstart SSERVICE NEW SERVIS " + class1.getName());
context.startService(new Intent(context, class1));
}
Run Code Online (Sandbox Code Playgroud)
并使用它startServiceOrNo.startServiceIfItsNotRuning(OfflineChopsMonitor.class,this)
如果我检查来自一个类的服务它的工作,但如果我检查来自不同类的相同服务,它的检查不起作用
我正在启动service =>后台服务,并开始检查"新线程"中的文件,在日志中我得到以下内容,服务/应用程序暂停.
记录: I/art: Explicit concurrent mark sweep GC freed 25935(1686KB) AllocSpace objects, 13(903KB) LOS objects, 39% free, 13MB/22MB, paused 649us total 43.569ms
它只是扫描SD卡中MyData中的文件,其中包含一堆图片(大约20个图片).
**扫描=获取图片名称并将其保存为字符串.
我有一个有两个服务的应用程序.
一个用于在其他应用程序上显示用于浮动(叠加)的UI WindowManager.另一种是使用位置跟踪GooglePlayAPI.我的应用总是运行这些服务.
我希望这些服务不被操作系统杀死.所以我打电话Service.startForeground().但是,通知抽屉中有两个通知.
有没有办法为这两种服务使用单一通知?
使用案例:将后台队列中的图像上传到服务器,图像可以是存储在手机内存中的网址或图像文件.
我想要的是将队列中的项目数量限制为3并将模糊图像显示为在活动中的回收站视图中上载的实际图像的占位符,每个占位符上都有一个进度条指示已上载了多少内容.在每个占位符的顶部有三个按钮,可以暂停,取消或恢复上传图像.
现状:现在,我是用Multipart在Retrofit 1.9.0上传图片和该服务呼叫在活动中完成的.
我无法弄清楚如何使用Retrofit或任何其他库来取消,暂停或恢复多部分POST请求以及如何将UI事件与api服务线程联系起来.我可以从服务更新UI,但是如何从UI中的事件(暂停/恢复/取消)更新服务中的某些内容?
我该如何处理这个用例?我需要使用服务吗?我可以根据服务中执行的请求在其他活动中显示进度指示器吗?这个过程的架构应该是什么?我不需要它的代码,但是如果有一些与此相关的有用的引用,我想阅读并测试它以最终得出我的方法.
首先:我知道ConnectivityManager.CONNECTIVITY_ACTION已被弃用,我知道如何使用connectivityManager.registerNetworkCallback.如果阅读JobScheduler,但我不完全确定我是否正确.
我的问题是,当手机与网络连接/断开连接时,我想执行一些代码.这应该在应用程序也在后台时发生.从Android OI开始,如果我想在后台运行我想要避免的服务,则必须显示通知.
我尝试获取有关手机何时使用JobScheduler/JobServiceAPI 连接/断开连接的信息,但它只会在我安排它时执行.对我来说,似乎在这样的事件发生时我无法运行代码.有没有办法实现这个目标?我可能只需要稍微调整一下我的代码吗?
我的JobService:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ConnectivityBackgroundServiceAPI21 extends JobService {
@Override
public boolean onStartJob(JobParameters jobParameters) {
LogFactory.writeMessage(this, LOG_TAG, "Job was started");
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork == null) {
LogFactory.writeMessage(this, LOG_TAG, "No active network.");
}else{
// Here is some logic consuming whether the device is connected to a network (and to which type)
}
LogFactory.writeMessage(this, LOG_TAG, "Job is done. ");
return …Run Code Online (Sandbox Code Playgroud)