问题描述
我正在制作一个应用程序,它读取在一个活动中通过蓝牙发送的数据,然后将其重定向到前台服务,该服务将意图发送到将处理数据的另一个活动。我知道我在活动代码上的广播接收器代码中遗漏了一些东西。
如果有人可以给我建议或帮助我处理数据。到目前为止没有崩溃。我是 android studio 的新手,任何帮助都会很棒!
public class ForegroundService extends Service {
public static final String CHANNEL_ID = "ForegroundServiceChannel";
@Override
public void onCreate(){
super.onCreate();
createNotificationChannel();
Intent notificationIntent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,notificationIntent,0);
Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentIntent(pendingIntent)
.build();
startForeground(1,notification);
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
//super.onStartCommand(intent,flags,startId);
String input = intent.getStringExtra("inputExtra");
Log.i("Tag",input);
sendData(input);
createNotificationChannel();
Intent notificationIntent = new Intent(this,CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setContentIntent(pendingIntent)
.build();
startForeground(1,notification);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent)
{
return null;
}
private void sendData(String input){
Log.i("Tag","inside sendData");
Intent intent = new Intent();
intent.setAction("com.example.Pillwoah.sendbroadcast");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.putExtra("inputExtra",input);
sendbroadcast(intent);
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID,"Foreground Service Channel",notificationmanager.IMPORTANCE_DEFAULT);
notificationmanager manager = getSystemService(notificationmanager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
*** 这是活动代码***
public class MainActivity5 extends AppCompatActivity {
protected static final String TAG = "TAG";
TextView dataText;
broadcastReceiver receiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
Log.i(TAG,"data sending");
configureReceiver();
}
class DatabroadcastReceiver extends broadcastReceiver {
@Override
public void onReceive(Context context,Intent intent) {
String message = "broadcast intent detected " + intent.getAction();
Log.i(TAG,message);
}
}
private void configureReceiver(){
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.Pillwoah.sendbroadcast");
receiver = new DatabroadcastReceiver();
registerReceiver(receiver,filter);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)