私は最近、アプリがテスト目的で閉じられている場合でも、5秒ごとに通知を表示するように試みているので、JobServiceを使用してそれを行います。
public class Job1 extends JobService {
private boolean cancelled = false;
public static final String tag = "Job";
private int id = 0;
private void createChannel(String id, CharSequence name, int imp) {
NotificationChannel channel = new NotificationChannel(id, name , imp );
NotificationManager nm = getSystemService(NotificationManager.class);
nm.createNotificationChannel(channel);
}
private void display(int id , CharSequence channel_id, String content, String title , Context ctx){
createChannel((String)channel_id , channel_id , NotificationManager.IMPORTANCE_HIGH);
NotificationCompat.Builder nb = new NotificationCompat.Builder(ctx, (String) channel_id)
.setSmallIcon(R.drawable.ic_work_black_24dp)
.setContentText(content)
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManagerCompat nmc = NotificationManagerCompat.from(ctx);
nmc.notify(id , nb.build() );
}
@Override
public boolean onStartJob(JobParameters params) {
Log.i(tag, "Job has been started");
Job(params);
return true;
}
private void Job(JobParameters params){
new Thread(new Runnable() {
@Override
public void run() {
while (true){
Log.i(tag, "Job is Running");
///////////////////////////////////////////////////
//the crash begin here.
display(id++ , "channel1" , "Description" , "title" , this);
////////////////////////////////////////////////////////////////
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
//jobFinished(params , true);
}
@Override
public boolean onStopJob(JobParameters params) {
Log.i(tag, "Job has been cancelled");
cancelled = true;
return true;
}
}
ジョブを呼び出すコード:
ComponentName cname = new ComponentName(this, Job1.class);
JobInfo jinfo = new JobInfo.Builder(1, cname)
.setRequiresCharging(false)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.build();
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int result = scheduler.schedule(jinfo);
if (result == JobScheduler.RESULT_SUCCESS) {
Log.i("job", "scheduled");
} else {
Log.i("job", "failed");
}
そしてもちろん、AndroidManifest.xmlに権限を持つサービス名も追加しました
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".Job1"
android:permission="android.permission.BIND_JOB_SERVICE" />
通知機能だけを使用しようとすると、たとえばボタンだけでそれを使用して完全にdisplay
機能しJob
、仕事で関数を呼び出す行を削除すると、通知を使用するとアプリ全体がクラッシュしますでは、何が問題になるのでしょうか?
logcatを見て注意深く読んだ後、私はようやく解決策を得ました
主な問題は、その中のスレッド自体にありましJobService
た。新しい通知を作成しようとすると、この情報でアプリがクラッシュします。
java.lang.RuntimeException: Can't create handler inside thread Thread[Thread-2,5,main] that has not called Looper.prepare()
解決策は、新しいスレッドクラスを作成して呼び出すことです Looper.prepare()
新しいスレッドコードはこのクラスになります:
public class TestThread extends Thread{
private int id = 0;
private Context ctx;
public TestThread(Context ctx){
this.ctx = ctx;
}
public void run(){
//this line has solved the issue
Looper.prepare();
while(true){
Log.i("Thread" , "Thread is currently running in background :)");
// my notifciation class
(new Notifications()).displayNot(id++ , "channel6", "Content" , "title" , ctx);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
そして今、私たちは仕事からこのクラスを使用しています、私の場合はJob
関数内:
private void Job(JobParameters params){
Thread thread = new TestThread(this);
thread.start();
if(!thread.isAlive()){
jobFinished(params , true);
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加