如何将意图从片段 class 传递到 android 中的服务 class
How to pass an intent from fragment class to a service class in android
我是 android 的新手,我正在学习 android 中的服务,我创建了一个片段,其中有一个 button.On 单击该按钮我需要显示 toast一条消息。
这是我的服务class -
public class BackgroundSoundService extends Service {
private static final String TAG = null;
@Override
public void onCreate() {
super.onCreate();
Log.d("tagg", "onCreate: inside service");
Toast.makeText(getApplication(), "This is my Toast message!", Toast.LENGTH_LONG).show();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我需要在我的片段 class 中的按钮被点击时吐司消息。
这是我的片段 class -
public class ConnectFragment extends Fragment {
Button service_btn;
public ConnectFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connect, container, false);
service_btn = (Button) rootView.findViewById(R.id.service_button);
service_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start_service();
Intent svc = new Intent(getActivity(), BackgroundSoundService.class);
try {
startActivity(svc);
} catch (Exception e) {
Log.d("tagg", "start_service:excception " + e);
}
}
});
return rootView;
}
public void start_service() {
Intent svc = new Intent(getActivity(), BackgroundSoundService.class);
}
}
现在我得到一个异常
start_service:excception android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.arjunh.navigationdrawer/com.example.arjunh.navigationdrawer.BackgroundSoundService}; have you declared this activity in your AndroidManifest.xml?
在您的 AndroidManifest.xml 文件中添加服务
<service android:name=".BackgroundSoundService">
然后从这样的片段开始服务
getContext().startService(new Intent(getContext(),BackgroundSoundService.class));
希望对你有所帮助。
我是 android 的新手,我正在学习 android 中的服务,我创建了一个片段,其中有一个 button.On 单击该按钮我需要显示 toast一条消息。
这是我的服务class -
public class BackgroundSoundService extends Service {
private static final String TAG = null;
@Override
public void onCreate() {
super.onCreate();
Log.d("tagg", "onCreate: inside service");
Toast.makeText(getApplication(), "This is my Toast message!", Toast.LENGTH_LONG).show();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我需要在我的片段 class 中的按钮被点击时吐司消息。 这是我的片段 class -
public class ConnectFragment extends Fragment {
Button service_btn;
public ConnectFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connect, container, false);
service_btn = (Button) rootView.findViewById(R.id.service_button);
service_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start_service();
Intent svc = new Intent(getActivity(), BackgroundSoundService.class);
try {
startActivity(svc);
} catch (Exception e) {
Log.d("tagg", "start_service:excception " + e);
}
}
});
return rootView;
}
public void start_service() {
Intent svc = new Intent(getActivity(), BackgroundSoundService.class);
}
}
现在我得到一个异常
start_service:excception android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.arjunh.navigationdrawer/com.example.arjunh.navigationdrawer.BackgroundSoundService}; have you declared this activity in your AndroidManifest.xml?
在您的 AndroidManifest.xml 文件中添加服务
<service android:name=".BackgroundSoundService">
然后从这样的片段开始服务
getContext().startService(new Intent(getContext(),BackgroundSoundService.class));
希望对你有所帮助。