我正在使用android studio的android应用程序中使用服务,它在清单文件中自动对其进行了声明。但是,当我在主页上注册服务后运行应用程序时,登录后该应用程序将崩溃。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alqahtag.foodappclient">
<application
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>
<activity android:name=".Login" />
<activity android:name=".SignUp" />
<activity
android:name=".Home"
android:label="@string/title_activity_home" />
<activity android:name=".Cart" />
<activity android:name=".FoodList" />
<activity android:name=".FoodDetail" />
<activity android:name=".OrderStatus" />
<service
android:name=".Service.$ListenOrder"
android:enabled="true"
android:exported="true"></service>
</application>
这是我的ListenOrder Java:
public class ListenOrder extends Service implements ChildEventListener {
FirebaseDatabase database;
DatabaseReference requests;
public ListenOrder() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
database = FirebaseDatabase.getInstance();
requests = database.getReference("Requests");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
requests.addChildEventListener(this);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//trigger here
Request request = dataSnapshot.getValue(Request.class);
showNotification(dataSnapshot.getKey(), request);
}
private void showNotification(String key, Request request) {
Intent intent = new Intent(getApplicationContext(), OrderStatus.class);
intent.putExtra("userPhone", request.getPhone());
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setTicker("Ghazwa")
.setContentInfo("Your order was update")
.setContentText("Order #" + key + " status is " + Common.convertCodeToStatus(request.getStatus()))
.setContentIntent(contentIntent)
.setContentInfo("Info")
.setSmallIcon(R.mipmap.ic_launcher);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled( DatabaseError databaseError) {
}}
并且我在首页上注册了服务意图。这是我的主页:(注册服务的onCreate方法)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
//Init Firebase
database = FirebaseDatabase.getInstance();
stores = database.getReference("Stores");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cartIntent = new Intent(Home.this, Cart.class);
startActivity(cartIntent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Set Name for User
View headerView = navigationView.getHeaderView(0);
textFullName = (TextView) headerView.findViewById(R.id.textFullName);
textFullName.setText(Common.currentUser.getName());
//Load menu
recycler_menu = (RecyclerView) findViewById(R.id.recycler_menu);
recycler_menu.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_menu.setLayoutManager(layoutManager);
loadMenu();
//Register Service
Intent service = new Intent(Home.this, ListenOrder.class);
startActivity(service);
}