(4.3.1.2)【项目一】主体框架Fragment中启动Service与notification的使用

创建Service

public class MyService extends Service {  

    public static final String TAG = "MyService";  

    @Override  
    public void onCreate() {  
        super.onCreate();  
        Log.d(TAG, "onCreate() executed");  
    }  

    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        Log.d(TAG, "onStartCommand() executed");  
        return super.onStartCommand(intent, flags, startId);  
    }  

    @Override  
    public void onDestroy() {  
        super.onDestroy();  
        Log.d(TAG, "onDestroy() executed");  
    }  

    @Override  
    public IBinder onBind(Intent intent) {  
        return null;  
    }  

}  

生成notification

// 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。  
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
        Notification myNotify = new Notification();  
        notify1.setLatestEventInfo(this, "Notification Title",  
                    "This is the notification message", pendingIntent);  
        manager.notify(NOTIFICATION_FLAG, notify1);  

示例

package com.android.yunshi.service;

//每日运程的信息推送
import android.app.Service;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.android.yunshi.LogoActivity;
import com.android.yunshi.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class DailyYunChengTuiSong extends Service {

    public Timer timer;
    final String TAG = "com.yhf.dailyyunchengtuisong";
    int CUSTOM_VIEW_ID = 1;// 通知的id号
    Calendar calendar;// 时间类,设定timer.schedule的执行时间
    Boolean isyunchengtuisong_Done;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onBind");
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        // TODO Auto-generated method stub
        super.onRebind(intent);
        Log.i(TAG, "onRebind");
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.i(TAG, "onCreate");
        timer = new Timer(true);// 当程序只有daemon线程的时候,它就会自动终止运行。
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Log.i(TAG, "onStart");

        // 设定执行时间为10:21分
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date time = calendar.getTime();
            // chedule方法,
            // 第一个参数是TimerTask对象
            // 第二个参数表示开始执行前的延时时间,或者定时
            timer.schedule(new TimerTask() {
                public void run() {
                    int icon = R.drawable.yunshi360;
                    CharSequence tickerText = "运势360";
                    long when = System.currentTimeMillis();

                    Notification notification = new Notification(icon,
                            tickerText, when);


                    RemoteViews contentView = new RemoteViews(getPackageName(),
                            R.layout.yunshi_more_tuisong_notification);
                    contentView.setImageViewResource(R.id.image,
                            R.drawable.yunshi360);
                    contentView.setTextViewText(R.id.title, "每日运程");
                    contentView.setTextViewText(R.id.text,
                            "记得开启运势360查看今日运程呦···");
                    notification.contentView = contentView;

                    Intent notificationIntent = new Intent(
                            DailyYunChengTuiSong.this, LogoActivity.class);
                    PendingIntent contentIntent = PendingIntent
                            .getActivity(DailyYunChengTuiSong.this, 0,
                                    notificationIntent, 0);
                    notification.contentIntent = contentIntent;

                    notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击后自动消失
                                                                        // 也可设置为不消失
                    notification.defaults =Notification.DEFAULT_SOUND;
                    /*

                    * 添加声音

                    * notification.defaults |=Notification.DEFAULT_SOUND;

                    * 或者使用以下几种方式

                    * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

                    * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

                    * 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT"

                    * 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音

                    */

                    /*

                    * 添加振动

                    * notification.defaults |= Notification.DEFAULT_VIBRATE;

                    * 或者可以定义自己的振动模式:

                    * long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒

                    * notification.vibrate = vibrate;

                    * long数组可以定义成想要的任何长度

                    * 如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动

                    */

                    /*

                    * 添加LED灯提醒

                    * notification.defaults |= Notification.DEFAULT_LIGHTS;

                    * 或者可以自己的LED提醒模式:

                    * notification.ledARGB = 0xff00ff00;

                    * notification.ledOnMS = 300; //亮的时间

                    * notification.ledOffMS = 1000; //灭的时间

                    * notification.flags |= Notification.FLAG_SHOW_LIGHTS;

                    */
                    String ns = NOTIFICATION_SERVICE;
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
                    mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
                    // 停止服务
                    // 服务停止的时间不确定,后面的代码还是会执行,并且onDestroy()方法也会执行,
                    // 下次重新启动服务的时候,先调用onCreate(),然后再调用onStart()方法。
                    DailyYunChengTuiSong.this.stopSelf();
                }
            }, time);
    }

}

注册Service

        <service android:name="com.android.yunshi.service.DailyYunChengTuiSong" >
            <intent-filter>
                <action android:name="com.yhf.dailyyunchengtuisong" >
                </action>

                <category android:name="android.intent.category.default" />
            </intent-filter>
        </service>

启动Service

  intent.setAction("com.yhf.dailyyunchengtuisong"); 
  startService(intent);
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值