2016-03-03 43 views
0

我无法显示通知。在MainaActivit中,我没有调用startService方法。我只能在后台服务中显示通知,即立即显示电话开始通知。当我调试错误显示在控制台,我在下面提到。无法在android工作室显示通知

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 
} 

BeaconService.Java

public class BeaconService extends Service { 
    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     showNotification(); 
     return START_STICKY; 
    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
    } 
    private void showNotification() { 
     NotificationCompat.Builder mBuilder = 
       (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.ic_loc) 
         .setContentTitle("Welcome to Brillio") 
         .setContentText("Hello Mansur, Welcome to Brillio.") 
         .setPriority(2) 
         .setOnlyAlertOnce(false); 
     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     mBuilder.setSound(alarmSound); 
     mBuilder.setOngoing(true); 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(2001, mBuilder.build()); 
    } 
} 

BeaconReceiver.java

public class BeaconReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent startServiceIntent = new Intent(context, BeaconService.class); 
     context.startService(startServiceIntent); 
    } 
} 

menifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<service 
      android:name=".BeaconService" 
      android:enabled="true" 
      android:exported="true" /> 
     <receiver android:name=".BeaconReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

错误显示在控制台

DEVICE SHELL COMMAND: am start -D -n "com.example.serviceexamp/com.example.serviceexamp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 
Client not ready yet. 
open: Permission denied 
open: Permission denied 

回答

0

清单文件中的服务类名称与您在检查后定义的服务类不同。

实际服务类:公共类BeaconService延伸服务在清单中定义

Service类:机器人:名字= “为MyService。”

+0

<服务 机器人:名称= “BeaconService。” 机器人:启用= “true” android:exported =“true”/> 否对于那个 – Alam

+0

可以放置整个清单文件的快照 – srinivas