我想在启动或启动android mobile时显示消息或通知。

时间:2016-02-29 04:41:45

标签: java android

我想在启动或启动Android手机时显示消息或通知。通知显示但是当我销毁应用程序时它没有显示消息或通知。所以请任何人帮助我。

public class BeaconService extends Service {
    Handler mHandler = new Handler();
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10000);
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                showNotification();
                            }
                        });
                    }catch (Exception e){

                    }
                }
            }
        }).start();
        return START_STICKY;
    }

    private void showNotification() {
        NotificationCompat.Builder mBuilder =
                (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_loc)
                        .setContentTitle("Welcome to Ford Show Room")
                        .setContentText("Hello Puneet, Welcome to Ford! You'll be shortly attended by Karthik! ")
                        .setPriority(2)
                        .setOnlyAlertOnce(true);
        Intent resultIntent = new Intent(this, SlideMenuActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(SlideMenuActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(2001, mBuilder.build());
    }
}

BeaconReceiverService.java

public class BeaconReceiverService extends BroadcastReceiver  {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("DEBUG", "Creating the intent");
        Intent service = new Intent(context, BeaconService.class);
        context.startService(service);
    }
}

menifest.xml中的服务

<service
            android:name=".BeaconService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </service>

        <receiver android:name=".BeaconReceiverService">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

1 个答案:

答案 0 :(得分:0)

以下这件事确实运作良好

的AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    >    

    <receiver android:name=".BootCompletedReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service android:name="NotifyingDailyService" >
    </service>

BootCompletedReceiver类

public class BootCompletedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    // TODO Auto-generated method stub
    context.startService(new Intent(context, NotifyingDailyService.class));
}

}

服务类

 public class NotifyingDailyService extends Service {

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

    @Override
    public int onStartCommand(Intent pIntent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Notification", Toast.LENGTH_LONG).show();

        return super.onStartCommand(pIntent, flags, startId);
    }
    }