enter code here
尝试启动该服务,但在第一次启动应用期间服务运行正常时,启动服务并未启动。
这是接收器,我想使用开关盒,因为我必须开始使用接收器。
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent;
switch (intent.getAction()) {
case SmackService.NEW_MESSAGE:
Log.i("TAG", "processMessage() BroadCast Recieve");
break;
case Intent.ACTION_BOOT_COMPLETED:
Log.i("TAG", "Boot");
Toast.makeText(context, "case", Toast.LENGTH_SHORT).show();
serviceIntent = new Intent(context, SmackService.class);
context.startService(serviceIntent);
break;
}
}
清单内容: -
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.abhishek.im.ChatReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.tlt.rx.msg" />
</intent-filter>
</receiver>
服务类
public class SmackService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
user_data = new HashMap<>();
if (intent != null && intent.hasExtra(BUNDLE_MSG_SYNC)) {
doChatSync = intent.getBooleanExtra(BUNDLE_MSG_SYNC, false);
}
if (intent != null) {
String email, password, name;
email = intent.getExtras().getString("email");
password = intent.getExtras().getString("password");
name = intent.getExtras().getString("name");
activity=intent.getExtras().getString("activity");
user_data.put("email", email);
user_data.put("password", password);
user_data.put("name", name);
start(user_data);
}
return Service.START_STICKY;
}
}
手动启动服务:
Toast.makeText(MainActivity.this, "Signing in", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, SmackService.class);
intent.putExtra("email", email);
intent.putExtra("password", password);
intent.putExtra("name", name);
intent.putExtra("activity", "MainActivity");
startService(intent);
NT);
答案 0 :(得分:1)
非常清楚发生了什么。在启动时,您的BroadcastReceiver
正在被调用。然后它会像这样开始Service
:
serviceIntent = new Intent(context, SmackService.class);
context.startService(serviceIntent);
在onStartCommand()
中执行此操作:
email = intent.getExtras().getString("email");
由于Intent
中没有“额外内容”,getExtras()
会返回null
而您在getString()
引用上调用null
,然后引发NullPointerException
Service
,导致#include<iostream>
using namespace std;
class Man
{
int stories;
public:
Man(int stories) : stories(stories) {cout << "A man is created with " << stories << " stories." << endl;};
};
class Grandpa : public virtual Man
{
int pipes;
public:
Grandpa(int stories, int pipes) : Man(1000), pipes(pipes)
{
stories += stories;
cout << "Grandpa is created with " << stories << " stories and pipes." << endl;
};
};
class Father : public Grandpa
{
int cars;
public:
Father(int stories, int cars) : Man(1000), Grandpa(1000, 1), cars(cars)
{
stories += stories;
cout << "Father is created with " << stories << " stories and cars." << endl;
};
};
class Son : public Father
{
int girls;
public:
Son(int stories, int girls) : Man(1000), Father(1000, 3), girls(girls)
{
stories += stories;
cout << "Son is created with " << stories << " stories and girls." << endl;
};
};
int main()
{
Son Dick(1000, 5);
return 0;
}
崩溃。
故事结束。