有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我希望在启动或启动安卓 mobile时显示消息或通知。

我希望在启动或启动安卓 mobile时显示消息或通知。通知显示,但当我销毁应用程序时,它不显示消息或通知。所以请任何人帮帮我

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。爪哇

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);
    }
}

梅尼费斯特的服务。xml

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

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

共 (1) 个答案

  1. # 1 楼答案

    下面这件事肯定很管用

    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);
        }
        }