5.31日15周学习内容总结

这周学习了安卓刚开始的时候的跳转的界面,还有安卓的自己自动按时向消息栏发消息;

这周的代码量在500行左右,这周各科的实验是比较多的,主要是忙各科的实验对于软件的敲写主要是PHP的网上商城了,跟web相似但是一些变量跟方法有一些不同,大概代码的编写量主要在500行左右。

public class MainActivity extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        Intent intent = new Intent(this, AutoReceiver.class);

        intent.setAction("VIDEO_TIMER");

                // PendingIntent这个类用于处理即将发生的事情               

                PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);

        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

                // AlarmManager.ELAPSED_REALTIME_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用相对时间

                // SystemClock.elapsedRealtime()表示手机开始到现在经过的时间

                am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,

                SystemClock.elapsedRealtime(), 10 * 1000, sender);

    }

}
public class AutoReceiver extends BroadcastReceiver {

    private static final int NOTIFICATION_FLAG = 1;

 

    @SuppressLint("NewApi")

    @Override

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("VIDEO_TIMER")) {            

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,

                    new Intent(context, MainActivity.class), 0);

            // 通过Notification.Builder来创建通知,注意API Level

            // API16之后才支持

            Notification notify = new Notification.Builder(context)

                    .setSmallIcon(R.drawable.ic_launcher)

                    .setTicker("TickerText:" + "您有新短消息,请注意查收!")

                    .setContentTitle("Notification Title")

                    .setContentText("This is the notification message")

                    .setContentIntent(pendingIntent).setNumber(1).build(); // 需要注意build()是在API

                                                   // level16及之后增加的,API11可以使用getNotificatin()来替代

            notify.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。

            // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。

            NotificationManager manager = (NotificationManager) context

                    .getSystemService(Context.NOTIFICATION_SERVICE);

            manager.notify(NOTIFICATION_FLAG, notify);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示

        }

    }

 

}
public class AutoReceiver extends BroadcastReceiver {

    private static final int NOTIFICATION_FLAG = 1;

 

    @SuppressLint("NewApi")

    @Override

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("VIDEO_TIMER")) {            

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,

                    new Intent(context, MainActivity.class), 0);

            // 通过Notification.Builder来创建通知,注意API Level

            // API16之后才支持

            Notification notify = new Notification.Builder(context)

                    .setSmallIcon(R.drawable.ic_launcher)

                    .setTicker("TickerText:" + "您有新短消息,请注意查收!")

                    .setContentTitle("Notification Title")

                    .setContentText("This is the notification message")

                    .setContentIntent(pendingIntent).setNumber(1).build(); // 需要注意build()是在API

                                                   // level16及之后增加的,API11可以使用getNotificatin()来替代

            notify.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。

            // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。

            NotificationManager manager = (NotificationManager) context

                    .getSystemService(Context.NOTIFICATION_SERVICE);

            manager.notify(NOTIFICATION_FLAG, notify);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示

        }

    }

 

}
<uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

<receiver            android:name=".AutoReceiver"            android:label="@string/app_name" >        
    <intent-filter>          
      <action android:name="android.intent.action.BOOT_COMPLETED" />                 <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>    
    </receiver>
原文地址:https://www.cnblogs.com/1234yyf/p/13032125.html