我开发了我的简单应用程序来尝试 Intent 服务。此应用程序从简单的编辑文本中给出一个数字,当用户按下“启动服务”按钮时,我的应用程序将启动我的服务:
public class MyService extends IntentService {
String name="MyService";
int i=0;
public MyService() {
super("MyService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
/*per esplorare il bundle
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d("KEY", String.format("%s %s (%s)", key,
value.toString(), value.getClass().getName()));
}
}*/
String value=intent.getStringExtra("i_key");
i=Integer.parseInt(value);
startTimer();
}
@Override
public void onStart(@Nullable Intent intent, int startId) {
Log.d("MyService","Start");
super.onStart(intent, startId);
}
private void startTimer() {
Log.d("Start Timer",String.valueOf(i));
final Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
if(i<10) {
i++;
Log.d("Timer",String.valueOf(i));
}
else {
Intent intent=new Intent("myIntent");
Log.d("Fine","FineTimer");
intent.putExtra("value",i);
sendBroadcast(intent);
timer.cancel();
}
}
};
timer.schedule(task,0l,1000);
}
@Override
public void onDestroy() {
Log.d("MyService","Chiusura Servizio");
super.onDestroy();
}
}
当我的输入大于 10 时,服务会向发送推送通知的广播接收器发送消息:
private BroadcastReceiver myBrodcast=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int i=intent.getIntExtra("value",0);
Intent myIntent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent intent1=PendingIntent.getActivity(getApplicationContext(),0,myIntent,PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification=new NotificationCompat.Builder(getApplicationContext(),NotificationChannel.ID)
.setSmallIcon(R.drawable.ic_not)
.setContentTitle("My_tile")
.setContentText(String.valueOf(i))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(intent1)
.build();
notificationManagerCompat.notify(1,notification);
}
};
MyChannelNotification 在此 App 中创建:
public class NotificationChannel extends Application {
public static final String ID = "My_id";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String desc = "This is my channel";
int Importance = NotificationManager.IMPORTANCE_DEFAULT;
android.app.NotificationChannel channel = new android.app.NotificationChannel(ID, name, Importance);
channel.setDescription(desc);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
这是我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.raffaelecoppola.service">
<application
android:name=".NotificationChannel"
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"></service>
</application>
</manifest>
当我在带有 26 SDK 的 Android 模拟器上启动我的应用程序时,出现此错误:
如何解决这个问题?感谢帮助